#include <iostream>
usingnamespace std;
int main()
{
constint findMin = 10;
constint MAXNUMS = 10;
int i, max, number[MAXNUMS], total=0;
for (i=0; i < MAXNUMS; i++)
{
cout << " enter a number : ";
cin >> number[i];
}
for (i = 0; i < MAXNUMS; i++)
{
cout << " " << number[i] << endl;
total = total + number[i];
}
max = number[0];
for (i = 1; i < MAXNUMS; i++)
if (max < number[i])
max = number[i];
cout << " The maximum value is " << max << endl;
system("pause");
return 0;
}
In that last code segment, you could replace int *index with int& index. By using a reference rather than a pointer you would save having to dereference it or having to get its address to pass to the function.
Do you mean you had errors with mof's code segment. It looks fine to me. Do you know how to use functions yet? If not perhaps you will have trouble with mof's code as it is supposed to be used as a separate function from main.
If you don't know about functions, they're really important so maybe you should look at the tutorial on this site:
Of course, your problem can be solved without introducing a new function (although in my opinion it would be better to use functions).
But surely you can just add a section identical to your section for finding the maximum, but with max replaced by another variable called min and replacing the line if(min < number[i]) with if (min > number[i]).
Hope this helps.
EDIT: Oh. I didn't realise findMin was a C standard library function...
There's two ways to pass arguments to a function: Pass-by-value or Pass-by-reference. Passing by pointer and reference fall under the pass-by-reference class. So in that sense there the same although tye do have their differences.
Yes, it would be good to look them up. The reason I suggested you use a reference is that it was possible to in the context, and references are slightly harder to break than pointers, I think.
guys i still have errors .
the errors are :
1) In function `int main()':
2) a function-definition is not allowed here before '{' token
3) expected `,' or `;' before '{' token
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main()
{
constint findMin = 10;
constint MAXNUMS = 10;
int i, max, number[MAXNUMS], total=0;
for (i=0; i < MAXNUMS; i++)
{
cout << " enter a number : ";
cin >> number[i];
}
for (i = 0; i < MAXNUMS; i++)
{
cout << " " << number[i] << endl;
total = total + number[i];
}
max = number[0];
for (i = 1; i < MAXNUMS; i++)
if (max < number[i])
max = number[i];
cout << " The maximum value is " << max << endl;
int ReturnMin(int *nums, int maxnum, int *index)
{
int i, min;
min = nums[0]; *index = 0;
for(i=1; i < maxnum; i++)
{
if(min > nums[i])
{min = nums[i]; *index = i;}
}
return min;
}
system("pause");
return 0;
}
venot, you are trying to define a function int ReturnMin inside the main function. You are not allowed to declare/define/etc functions within other functions.
You should move the definition of ReturnMin outside main. If you put it below main, you will need to declare it above main like this: int ReturnMin(int* nums, int maxnum, int * index);
If you don't understand this declaration thing, I suggest you just put the ReturnMin function above the main function. However I also suggest you read fully the tutorials at cplusplus.com on functions so you can understand declaration of functions.
EDIT: It's important that rather than just be told how to fix it, you actually understand why it needs to be fixed. Basically, functions cannot be defined within each other. Read the tutorials for lots of information on functions.
#include <iostream>
#define MAX 100
usingnamespace std;
void max (int v[], int n);
int main () {
int i;
int n ;
int v[MAX];
cout<<"Size of the array: ";
cin>>n;
int member;
for (i = 0; i <n; i ++) {
cout<<"Enter member of the array with index: "<<i<<"\n"<<endl;
cin>>member;
v[i] = member;
cout<<"\n";
}
max(v, n);
cin.get(); cin.get();
return 0;
}
void max(int v[], int n) {
int maxx = v[0];
int i;
for (i = 0; i < n; i ++)
if (v[i]>maxx)
maxx = v[i];
cout<<" Max value in the array: "<<maxx<<endl;
}