Can't get variable to work in a function?

I am trying to get my program to find the min and max value of an array, I tested the array to make sure it filled on line 71, but then when I tried to use it in the functions maxValue and minValue it no longer recognises them?
Any ideas would be appreciated

#include <iostream>
#include <fstream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;
// function to find the maximum value;
int maxValue( numArray ) {

int mxm = numArray[0];
int i;
for (i=0; i<numArray.length; i++) {

if (numArray[i]>mxm) {

mxm = numArray[i];

}

}
return mxm;

}
// function to find the minimum value;
int minValue( numArray ) {

int min = numArray[0];
int i;
for (i=0; i<numArray.length; i++) {

if (numArray[i]<min) {

min = numArray[i];

}

}
return min;

}
int main (int argc,char* argv[])
{
// check to see what argv's are
cout << "argc = " << argc << endl;
for(int i = 1; i < argc; i++)
cout << "argv[" << i << "] = "
<< argv[i] << endl;


//Convert incoming char*'s to list of integers and fill array
int numArray[10];
int i;
for(i=1;i<=(argc-1);++i)
{
numArray[i]=atoi(argv[i]);
}
for(i=1;i<=(argc-1);++i)
{
cout<<numArray[i];
}




cout<<numArray[2];
cout<<maxValue;
cout<<minValue;
}
How did you test this? It won't even compile, unless it is not in C or C++.
Sorry I was trying to compile it on Visual Studio 2010 but it has errors with the numArray variable in the functions.
Thanks
- numArray has no type in the function's parameter list (in minValue and maxValue)
(it should probably be int __Value(int numArray[])

- also an array is not a class, it does not have members, and you cannot get an array's size by typing array.length

- youre also not passing any arguements to maxValue and minValue when you try to call them in main
(cout << ___Value(numArray);)

- youre not printing argv[i] near the start of main, youre printing i

when youre done fixing all that, post your code again =)
Last edited on
Thanks I got most of what you said done but I cannot find a way of setting the length. I tried making a variable arraySize that equals (argc-1) but that did not work either.

#include <iostream>
#include <fstream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;
// function to find the maximum value;
int maxValue(int numArray[] ) {

int mxm = numArray[0];
int i;
for (i=0; i<arraySize; i++) {

if (numArray[i]>mxm) {

mxm = numArray[i];

}

}
return mxm;

}
// function to find the minimum value;
int minValue(int numArray[] ) {

int min = numArray[0];
int i;
for (i=0; i<arraySize; i++) {

if (numArray[i]<min) {

min = numArray[i];

}

}
return min;

}

int main (int argc,char* argv[])
{
// check to see what argv's are
cout << "argc = " << argc << endl;
for(int i = 1; i < argc; i++)
cout << "argv[" << i << "] = "
<< argv[i] << endl;

int arrraySize=(argc-1);
//Convert incoming char*'s to list of integers and fill array
int numArray[10];
int i;
for(i=1;i<=(argc-1);++i)
{
numArray[i]=atoi(argv[i]);
}
for(i=1;i<=(argc-1);++i)
{
cout<<numArray[i];
}




cout<<numArray[2];
cout<<maxValue(numArray);
cout<<minValue(numArray);
because the functions are now missing the " arraySize " parameter, youll need to add that just like you did with numArray, and then pass them to maxValue and minValue just like you did at the end of your code with numArray

Also you dont need all those headers, just iostream is enough i believe
Ok I get what you said and thanks so much. It compiled with the changes but the minValue is showing the wrong number?
Can I change this line to be whatever argc-1 is? int numArray[10];
Thanks for the help


#include <iostream>

using namespace std;
// function to find the maximum value;
int maxValue(int numArray[],int arraySize ) {

int mxm = numArray[0];
int i;
for (i=0; i<arraySize; i++) {

if (numArray[i]>mxm) {

mxm = numArray[i];

}

}
return mxm;

}
// function to find the minimum value;
int minValue(int numArray[],int arraySize ) {

int min = numArray[0];
int i;
for (i=0; i<arraySize; i++) {

if (numArray[i]<min) {

min = numArray[i];

}

}
return min;

}

int main (int argc,char* argv[])
{
// check to see what argv's are
cout << "argc = " << argc << endl;
for(int i = 1; i < argc; i++)
cout << "argv[" << i << "] = "
<< argv[i] << endl;

int arraySize=(argc);
//Convert incoming char*'s to list of integers and fill array
int numArray[10];
int i;
for(i=1;i<=(argc-1);++i)
{
numArray[i]=atoi(argv[i]);
}
for(i=1;i<=(argc-1);++i)
{
cout<<numArray[i];
}
cout<<numArray[2]<<endl;
cout<<" The maximum number in the list is "<<maxValue(numArray,arraySize)<<endl;
cout<<" The minimum value in the list is "<<minValue(numArray,arraySize)<<endl;
/*

//cout<<" The minimum value in the set of integeres is ";


}
Have made a few changes.Changes are underlined.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
using namespace std;

// function to find the maximum value;

int maxValue(int numArray[],int arraySize )
{

	int mxm = numArray[0];
	int i;
	for (i=0; i<arraySize; i++) {
	if (numArray[i]>mxm) {
		mxm = numArray[i];
			     }
				    }
return mxm;
}
// function to find the minimum value;
int minValue(int numArray[],int arraySize ) {
	int min = numArray[0];
	int i;
	for (i=0; i<arraySize; i++) {
	if (numArray[i]< min) {
	min = numArray[i];
			     }
				     }
return min;
}

int main (int argc,char* argv[])
{
// check to see what argv's are
cout << "argc = " << argc << endl;
for(int i = 1; i <= argc; i++)
cout << "argv[" << i << "] = "
<< argv[i] << endl;

int arraySize=(argc);
//Convert incoming char*'s to list of integers and fill array

int numArray[10];
int i;
for(i=1;i<=(argc);++i)
{
numArray[i]=atoi(argv[i]);
}
for(i=0;i<=(argc-1);++i)
{
cout<<numArray[i];
}

cout<<" The maximum number in the list is "<<maxValue(numArray,arraySize)<<endl;
cout<<" The minimum value in the list is "<<minValue(numArray,arraySize)<<endl;

}
Last edited on
Thanks. I still cannot get minValue to work. I do not get it , it looks identical to maxValue that does work.
You have problems with array indexing - in particular where to start and where to stop.
Try a different tact - here it is - try and figure out why this works and yours don't.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
using namespace std;

// function to find the maximum value;

int maxValue(int numArray[],int arraySize )
{

    int mxm = numArray[0];
    int i;
    for (i=0; i<arraySize; i++) {
        if (numArray[i]>mxm) {
            mxm = numArray[i];
        }
    }
    return mxm;
}
// function to find the minimum value;
int minValue(int numArray[],int arraySize ) {
    int min = numArray[0];
    int i;
    for (i=0; i<arraySize; i++) {
        if (numArray[i]< min) {
            min = numArray[i];
        }
    }
    return min;
}

int main (int argc,char* argv[])
{
    // check to see what argv's are
    cout << "argc = " << argc << endl;
    for(int i = 1; i < argc; i++)//<<===============
        cout << "argv[" << i << "] = "
        << argv[i] << endl;

    int arraySize=(argc-1);//<<===================
    //Convert incoming char*'s to list of integers and fill array

    int numArray[10];
    int i;
    for(i=1;i<(argc);++i)
    {
        numArray[i-1]=atoi(argv[i]);//<<======******** important line
    }
    
    
    for(i=0;i<(argc-1);++i)//<<===========
    {
        cout<<numArray[i] << endl;
    }


    cout<<" The maximum number in the list is "<<maxValue(numArray,arraySize)<<endl;
    cout<<" The minimum value in the list is "<<minValue(numArray,arraySize)<<endl;

}


For the record, I was assuming you run the program passing the values something like this:
program_name 1 2 3 4 5 6
Last edited on
Thanks, So I was starting the array but not filling the first position, so I got an address value?
Is there a way of making "int numArray[10]" something more like "int numArray[argc-1]"
Last edited on
Yes. Using dynamic memory/allocation.
http://www.cplusplus.com/doc/tutorial/dynamic/
Topic archived. No new replies allowed.