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];
}
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];
}
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 ";
#include <iostream>
usingnamespace 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;
}
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.
#include <iostream>
usingnamespace 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
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]"