i need some basic guides to write this program

i have problem's with how to write a program that will find the largest and smallest integer numbers and their location in a set of numbers
how to add find min numbers coding in this program ??

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
#include <iostream> 
using namespace std;


int main()
{
  const int findMin = 10;
  const int 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;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
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;
}
Last edited on
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.

Just a small change though ;) Hope this helps.
i cant run it...it still have errors ..any guides on using findMin ?
you just have to include..#include <cstdlib> xD
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:

http://cplusplus.com/doc/tutorial/functions/
http://cplusplus.com/doc/tutorial/functions2/

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...
Last edited on
Thanks Xander314 for the tip. I've never passed by reference before, only by pointer, so what is the difference?:)
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.
OK I guess I'll look it up. It's about time I stopped being confused with the different syntax.
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

the codes :
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> 
#include <cstdlib>
using namespace std;


int main()
{
  const int findMin = 10;
  const int 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.

Hope this helps.
can u show me in which line i have to fix it ?
The whole function int ReturnMin from lines 38 to 50 should be moved before int main().

You should really read up on functions here http://www.cplusplus.com/doc/tutorial/functions/ and here http://www.cplusplus.com/doc/tutorial/functions2/

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.
Last edited on
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
#include <iostream>
#define MAX 100
using namespace 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;
}


	
Topic archived. No new replies allowed.