C :: Integers Are Not Passed Through Functions
Nov 12, 2013
I am trying to create a program that reads my file filled with random words, it then compares the words after they are put into a 2d array and sees if there is any matching words.. unfortunately the count is not working for me (in function2 and function3) and I am not sure why..
Code:
#include<stdio.h>
#include<string.h>
char function1(char words_array[][17]);
int function2(char words_array[][17]);
void function3(int pairs, char words_array[][17]);
int main( void )
{ char words_array[20][17];
int x = 0;
[Code]...
View 6 Replies
ADVERTISEMENT
Dec 14, 2014
I am writing a code that creates a deck of cards using a doubly linked list. One function, newDeck(), is made to create a new deck if the user wants. I don't have any problems creating the deck and it all seems to work fine, but when I run the whole program and a separate function needs to utilize the nodes in my deck, I get an error. Trying to find where there was a problem, I tried displaying the values of a card node in random parts of my code. At the end of the newDeck function, after the whole deck has been created, the card still displays correctly, yet when I return back to main immediately after newDeck has been called, I noticed that the values of my cards changed to either random values or they became null. I found this strange because in between the end of my newDeck function and at this point, there is no extra code so it doesn't seem as if there is any way the pointers could have been changed. Maybe It's because I don't have a complete understanding of pointers yet but is there any way that pointers can change values when returning from a function to main?
newDeck(head, tail, n); //this is how I call the function from main.
void newDeck(Card* head, Card* tail, Card* n) //this is my function body
{
for (int i = 0; i < 4; i++)
{
[Code]....
View 2 Replies
View Related
Dec 4, 2014
I missed last class on doing void functions because I got sick and im completely lost! ive being using the texts book example for a reference but its not running !
The output should look similar to this:
how much was your shirt?
20
shirt 20.00
tax =1.20
the total 21.20
Code:
include <iostream>
#include <iomanip>
using namespace std;
void getShirtCost(double shirtCost);
void calculate(double shirtCost,double taxAmount, double total, double taxamount) ;
void printReceipt(double shirtCost, double taxAmount, double total, double taxamount);
[Code] ....
View 1 Replies
View Related
Sep 13, 2014
I am supposed to update the value temp without using a return statement, or a global variable. I have never ran across a problem like this before and I am totally stuck. I think I'm missing something really simple and need a hint. Here is the code that was provided for "fixing".
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
[Code]....
View 2 Replies
View Related
Mar 22, 2013
I am posting this simplified piece of code that is a bit confusing for me. There are two functions that I call. One shows the expected results but the result of the other one rather puzzles me.
//#define defineVecTyp Vec3f
#define defineVecTyp float
template <typename vecTyp>
vecTyp buildLaplacianPyramid(cv::Mat inputmat) {
vecTyp lapPyr;
[Code].....
Calling the function sum1 does not change the values stored in the variables val1 and val2. The output of the program is as follows:
val1= 1 ## val2= 10 // before the call of function sum1
val1= 1 ## val2= 10 // after the call of function sum1
sumOfVals= 22
This is quite obvious and as expected and I just pasted this piece of code as an example for better clarification.
However, if I call the function buildLaplacianPyramid and apply a function for Gaussian Blurring, this also effects the cv::Mat passed to the function. The line imshow("M1, after buildLaplacianPyramid",M1); therefore shows an image that is blurred. Since I am not passing a pointer to the cv::Mat I do not understand why this should be happening. I was assuming that there would be a copy of the cv::Mat M1 to be used within the function. Therefore I was expecting the cv::Mat M1 to retain its original value. I was expecting that all changes applied to cv::Mat inputmat within the function would not have any influence on the cv::Mat M1. Just like in my other example with the sum.
View 3 Replies
View Related
Apr 5, 2013
from k&r book (4.2 - functions retuning non integers)
(doesnt compile - 1 error)
Code:
#include <stdio.h>
#define MAXLINE 100
/* rudimentary calculator */
main()
{
double sum, atof(char []);
char line[MAXLINE];
int getline(char line[], int max);
sum = 0;
while (getline(line, MAXLINE) > 0)
printf(" %g
", sum += atof(line));
return 0;
}
Code:
int getline(char line[], int max); is no different from:
Code:
getline(char line[], int max); ?
so, the int (return type) is optional when using a function. i assume return type is casted double? why the (char [])? just weird cuz i never seen it before
View 2 Replies
View Related
Mar 29, 2014
current functions are:
char *createEnvironment(int width, int height, int numWoodChips);
void printEvrionment(char *environment, int width, int height);
need to be:
int* createEnvironment(int width, int height, int numWoodChips);
void printEvrionment(int* environment, int width, int height);
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
[Code]....
View 1 Replies
View Related
Nov 7, 2014
I am working on a number of utility functions for two dimensional arrays of integers, or matrices. However I am having a problem with segmentation faults, most likely due to errors in using malloc in functions like copyMatrix.
Code:
matrix_utils.h~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//This function checks if two matrices are equal
int isEqual(int **A, int **B, int n);
//This function returns one row of a matrix
int * getRow(int **A, int i, int n);
//This function returns one column of a matrix
int * getCol(int **A, int j, int n);
[Code] ....
View 2 Replies
View Related
Apr 21, 2014
I'm a beginner at c++ and I need to write a program that reads a set of integers and then finds and prints the sum of the even and odd integers. The program cannot tell the user how many integers to enter. I need to have separate totals for the even and odd numbers. what would I need to use so that I can read whatever number of values the user inputs and get the sum of even and odd?
View 2 Replies
View Related
Mar 20, 2014
I'm a little confused by my programming assignment this week. I've been working at it Wednesday and I've made progress but I'm still confused as to how I'm supposed to do this. The class I made is called Stack, and it's derived from a template class called StackADT. We also utilize a class called unorderedLinkedList, which is derived from a class called linkedList.
We're supposed to implement all of the virtual functions from stackADT in the Stack class. The Stack data is stored in a an unorderedLinkedList, so what I'm confused by is how to implement a few of the Stack functions because there are no functions in unorderedLinkedList which we could call to manipulate the data.
As you can see from my attached code, I'm really confused by how I'm supposed to implement the pop() and top() functions, and I also think my initializeList() function is wrong. We don't have any similar functions in unorderedLinkedList to call, so I'm at a loss of how i'd access my unorderedLinkedList. My initial thought was to call the similar functions in the class that unorderedLinkedList was derived from, linkedList, but I'm unsure of this is what we're supposed to do, or if theres actually a way to access my unorderedLinkedList without having to use the functions from the base class.
NOTE: We're not allowed to modify stackADT, unorderedLinkedList, and linkedList.
Stack.h
#include "stackADT.h"
#include "unorderedLinkedList.h"
template<class Type>
class Stack: public stackADT<Type>{
template <class T>
struct nodeType
{
T info;
nodeType<T> *link;
[Code]...
View 3 Replies
View Related
Feb 4, 2014
Draw the total of a passed value, or value in general, without checking how many values there are individually.
Here's a pseudo-example:
int aliens = 10; // This should be changed as desired.
void drawaliens(int value) {
aliens.draw(value); // Instead of having a test of how many to draw, draw the amount there is from one check
}
I want to draw the amount of aliens passed or called to the alien draw function.
However, I don't want the function to check every possible value of aliens which could be passed before drawing, and just check the value once and draw that value.
IF x ALIENS, DRAW x ALIENS instead of...
IF 1 ALIEN, DRAW ALIEN(1);
IF 2 ALIEN, DRAW ALIEN(2);
IF 3 ALIEN, DRAW ALIEN(3);
.... and so on.
If there can be hundreds of aliens, it seems impractical to check every single possible value before drawing, and just check the value and draw that value.
View 2 Replies
View Related
Apr 12, 2014
So here I have a program that is supposed to basically sort out who passed and failed. I am currently stuck on the switch statement, I'm trying to count how many students received an A, B, C, etc.
Here's what I have so far:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void printIdInfo(ofstream &fout);
void printColumnHeaders(ofstream &fout);
[Code] ....
View 1 Replies
View Related
Aug 25, 2013
I have a function :
void UpdateNotefications(vector <string> &Notefic)
Where I change a vector...
I called it with a thread :
thread t1 (UpdateNotefications, Notefic);
t1.detach();
Though I see that inside the func, Notefic has a val, and outside, it is still empty (after the function is over)...
View 6 Replies
View Related
Sep 28, 2014
I am going to read some codes about image processing and I need to understand functions like this one below?
BOOL Trans_Geo_Affine(CImage *pImgSrc, CImage *pImgDst, BOOL bMatrix,
BOOL bHvInversed, double fScale_xx, double fFlex_xy, double fSkew_yx,
double fRotate_yy, double fTx, double fTy, int nInterpolation, BOOL bResize, BYTE cFill)
[URL]
two parameters, CImage *pImgSrc and CImage *pImgDst. I think they are class pointers and the function is passed by reference. What should I learn to understand this function and its parameters? How should I use this function? how to use the function with two parameters CImage *pImgSrc and CImage *pImgDst.
View 11 Replies
View Related
Feb 9, 2015
I have this code:
tgaManager.fileWriter.put((unsigned char)min(blue*255.0f,255.0f)).put((unsigned char)min(green*255.0f, 255.0f)).put((unsigned char)min(red*255.0f, 255.0f));
that should pass the value decided by the min function to an ofstream object, filewriter, that call the put method to print chars in a tga image file. When I open the file, all I see it is a huge black screen. You may be thinking that the values of blue,green and red are all zero but it is not the case.
With this code:
if (x==50 && y==50) {
cout << "Min BGR: " << endl;
cout << min (blue*255.0f,255.0f) << ' ' << min (green*255.0f,255.0f) << ' ' << min (red*255.0f,255.0f) << ' ' << x << ' ' << y << endl;
I get: Min BGR: 9.54167 29.9188 47.8701 50 50
View 3 Replies
View Related
Apr 8, 2015
I have a list of Strings that are passed to a method consecutively by reference from a class. I want to get the string value passed to the method at a point in time. The reason is to perform an if statement.
//this is the class that holds that holds the constants.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace xxx.Functional.xyz.Login {
public class Constants {
public static String Username = "paul";
public static String Code = "4";
[code].....
View 2 Replies
View Related
May 21, 2014
Is it correct for me to make a clone of testobj in function AddTest below, before i add it to my map? What i want is an user pass testobj to me though AddTest, and after i add it into my map, i do not want to have anything to do with the original testobj anymore. I.e, they are two copies, one belong to Device, one belong to the caller, both has no link to each other.
Also regarding the GetTest method, i prefer to return a raw pointer, as i do not want to force the caller to use smart pointer. And i also do not want the caller to be able to do anything that may change testobj in the map. So i am not sure to return as const reference or make a clone of testobj on the map and return it to the user (but caller need to delete the testobj when it is not used).
Code:
class Device {
public:
void AddTest(const Test* const testobj) {
_pTest.insert("ss", QSharedPointer<Test>(test->clone()));
} const Test* const GetTest(QString str) {
return _pTest[str].data();
[code].....
View 6 Replies
View Related
Jun 18, 2013
Here is the code,
Code:
vector<int> vec;
for(int i=0;i<10;++i)
vec.push_back(i);
random_shuffle(vec.begin(), vec.end());
nth_element(vec.begin(), vec.begin()+7, vec.end());
No matter how I choose the second argument passed to nth_element, the elements in vec are always sorted as,
0,1,2,3,4,5,6,7,8,9
What is the use of second argument here?
View 8 Replies
View Related
May 3, 2013
At the moment im trying out with pointing to an array of functions. I got this working as following:
typedef void (* functionPtr) ();
functionPtr functions[2][2]={{do11,do12}, {do21,do22}};
void do11(){DEBUG_PRINTLN("11");}
void do12(){DEBUG_PRINTLN("12");}
void do21(){DEBUG_PRINTLN("21");}
void do22(){DEBUG_PRINTLN("22");}
void loop(){
A=0;
B=1;
functions[A][b]();
}
But now I'm trying to use this to point to a function inside a class so instead of do11, i want to be able to point to Basic.Do11. Somehow this doesnt work and I keep on getting this message:
error: argument of type 'void (Basic::)()' does not match 'void (*)()'
View 2 Replies
View Related
Nov 17, 2014
I noticed that when using variadic functions, if I pass the va_arg() as parameter to a function, the parameters get passed in reverse. Is that expected?
For example, the following code outputs
Code:
1 2
2 1
Code:
#include <iostream>
#include <stdarg.h>
void foo_func(int v1, int v2)
{
std::cout << v1 << " " << v2 << std::endl;
[Code] .....
View 3 Replies
View Related
Feb 20, 2013
I have to write a c++ program with my own function which consists of two parameters (day, month). Function have to return number of days since the begining of this year. Using this function i have to find out how many days are left till birthday (or how many days have passed since last birthday)
This is how far i am:
Code:
#include <iostream>
using namespace std;
int cikDienu(int diena, int menesis);
int main()
[Code] ....
View 9 Replies
View Related
Dec 24, 2014
when i pass a string pointer to a function such as string *str = new string(""); and pass that string to a handleElement() function e.g. handleElement(str), and i change the value in the function it simply doesn't change when the function exits as though it's passing it by value or something, even though it gives the pointer address.. I've now changed the code to use double pointers and pass the reference of the str pointer and it works but it seems ugly to use double pointers for this.
//handles when a new element is encountered when parsing and adds it to the parse tree
bool ParseBlock::handleElement(char cur, string *curString, int count, bool isOperator) {
countNode = new ParseNode(count);
//keep track of numbers and strings if they exist and insert them
if(!curString->empty()){
if(isNumber(*curString)
[code].....
View 2 Replies
View Related
Dec 8, 2014
I have to write an example in which you use a function having as argument a pointer passed by reference in C++. Can you provide an example like this:
funz.h : void funz( int *&a );
funz.cpp : ? (1)
main.cpp:
#include "funz.h"
#include <iostream>
[Code]...
as I write in (1) and (2) ?
View 5 Replies
View Related
Mar 9, 2013
I currently have multiple functions that use globally declared arrays in my code.
I want to turn them so that arrays are no longer globally declared, but instead are passed by references to the function.
And I have a function caller inside main, and other functions are called within the function.
View 3 Replies
View Related
Apr 23, 2013
Essentially, the 'Sequence' below uses linked lists to store data. If 'result' refers to the same sequence as 'seq1' or 'seq2', I want 'result' to refer to a new sequence. This new sequence can be default constructed (no copy of 'seq1' or 'seq2' is required). I can't seem to do this correctly. Also, the prototype of the function cannot be altered.
void fun(const Sequence& seq1, const Sequence& seq2, Sequence& result) {
// Check for reference to same sequence. If they are the same,
// create new sequence for 'result' to refer to
if ((&seq1 == &result) || (&seq2 == &result)) {
[Code] ......
View 4 Replies
View Related
Apr 18, 2015
I'm trying to dynamically allocate a standard array at runtime in the function of a class where the array is "owned" by the calling class. The calling class knows nothing about the array before it makes the call to create the array other than the datatype of the array. But the full array of data needs to be returned.
It appears that the pointer being passed makes a copy of the pointer on the stack and then when the function returns it pops it off the stack and the array is a memory leak because the pointer is once again a nullptr as it was before being passed and the array has not been deallocated with delete yet (as it should not have been).
(Edit:Unexpected value of MyArray being a nullptr instead of pointing to an array after returning from line 09.)
class Class1 {
void FunctionA() {
Class2 OwnedClass;
int* MyArray = nullptr;
int SizeOfMyArray = 0;
[Code] ....
View 14 Replies
View Related