help with arrays

I need help writing a program that ask a user to enter 10 values into an array and then display the largest and the smallest of the values entered, so far this is all I have and I dont know how to label each of the values to show smallest and largest.
Any help would be greatly apreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  int main()
{
    const int arraySize = 10;
          int value[arraySize];

     for (int count = 1; count <= arraySize; count++)
        {
         cout << "Please enter the value for element number " << count << endl;
         cin >> value[count];
        }

      for (int count1 = 1; count1 <= arraySize; count1++)
          {cout << value [count1] << endl; }
          
This is a function that takes array p[] in and a a number of items in the array - m.
Then it uses the local varaibles largestsofar and smallest so far to find the largest and smallest grades within the array. This works for grades (integers)



void findrange(int p[], int m) {

int largestsofar, smallestsofar, distance;

largestsofar = p[0];
smallestsofar = p[0];

for(int count=1; count<m; count++) {

if(largestsofar<p[count])

largestsofar = p[count];

if(smallestsofar>p[count])


smallestsofar = p[count];
}

distance = largestsofar - smallestsofar;

cout << "The highest grade is " << largestsofar << " and the smallest grade is " << smallestsofar << endl << endl;

cout << "The difference between the largest and the smallest is: " << distance << endl << endl;

return;




thanks! Im going see if I can get it to do what I need.
First of all arrays start at index 0 not 1 so fix line 6 and 12 and in your cout do count +1 I would suggest and second the arrayends at 9 because it is set at a size of 10 and not 11 and linew 13 willdisplay all items if you want to check large/small try something like
1
2
3
4
5
int small=value[0];
For(int I=0; I<10; I++){
     If(value[I]<small)
          Small= value[I];
} 


On phone so might be a few typos or uppercase on accident
Last edited on
Just to let you know, arrays start at 0. So the loop should be count = 0; count < arraySize; count++
Well, you aren't using every position in your array:
your array size is 10, which means it has an index of {0,1,2,3,...8,9}, starting from zero up to 9, which gives you a total of 10.
But the way your for loop is working, is at one point count will be equal to 10 and you will be trying to store data into position value[10] of your array, but as noted above, isn't allocated for your array, and could give you possible errors. A better way to go about it would be.

1
2
3
4
5
 for (int count = 0; count < arraySize; count++)
        {
         cout << "Please enter the value for element number " << count+1 << endl;
         cin >> value[count];
        }

that will still give you same output, but without going out of bounds on your array.

and as for finding the larges and smallest values in your array:

1
2
3
4
5
6
7
8
9
10
11
//declare variables for smallest and largest
int smallest = 0, largest = 0;

//for loop to find the smallest and largest
for (int count1 = 0; count1 < arraySize; count1++){
   if (smallest > value[count1])
     smallest = value[count1]

  if(largest < value[count1])
     largest = value[count1])
} 


try something like that
ok so after doing all of that how do I cout the largest and the smallest. I tried cout << largest; and it couts 0.
Last edited on
that is an updated version of the program everything is working except for the smallest value is giving me 0.
I know is something small but I just dont see it.

const int arraySize = 10;
int value[arraySize];
int element;
int smallest = 0, largest = 0;
// for loop to assign a value to each element
for (int count = 0; count < arraySize; count++)
{
cout << "Please enter the value for element number " << count + 1 << endl;
cin >> value[count];

}

//for loop to find the smallest and largest
for (int count1 = 0; count1 < arraySize; count1++)
{
if (smallest > value[count1])
smallest = value[count1];

if(largest < value[count1])
largest = value[count1];
}

cout << "smallest " << smallest << endl;
cout << "Largest " << largest << endl;
Yeah, because I initilized smallest to 0, and there is no number small then zero in your array, then you will always get zero.

To fix this:
1
2
3
4
5
6
7
8
9
10
11
12
13
//declare variables for smallest and largest
int smallest = 0, largest = 0;

smallest = value[0];  // assigning a value from the array to smallest
largest = value[0];    // assigning a value from the array to smallest
//for loop to find the smallest and largest
for (int count1 = 0; count1 < arraySize; count1++){
   if (smallest > value[count1])
     smallest = value[count1]

  if(largest < value[count1])
     largest = value[count1])
} 
ok I got it. I had to assign a larger number to smallest, went from 0 to 10000
Thanks guys.
That will work, but if you every get an array with 10000 being the smallest number you will have the same issue.
You will be better off just assigning smallest, to a number in the array.

 
smallest = value[0];
That's what I said earlier :p
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
#include<iostream>
using std::cout;
using std::cin;
using std::endl;


int main(){

int
        myArray[10],
        smallest,
        largest;

cout<<"Enter 10 numbers please :\n";

for(int index=0;index<10;index++){
        cout<<"Enter number "<<index+1<<": ";
        cin>>myArray[index];
}//end loop for

for(int index=0;index<10;index++){
        if(index==0){
                smallest=myArray[index];
                largest=myArray[index];
        }else{
                if(myArray[index]<smallest)
                        smallest=myArray[index];
                if(myArray[index]>largest)
                        largest=myArray[index];
        }//end if...else

}//end loop for

cout<<"\n\nThe largest number is: "<<largest
        <<"\nThe smallest number is: "<<smallest
        <<endl;


return 0; //indicates successful termination
}//end main 


Enter 10 numbers please :
Enter number 1: -1
Enter number 2: -2
Enter number 3: -3
Enter number 4: -4
Enter number 5: 10
Enter number 6: -5
Enter number 7: -7
Enter number 8: -6
Enter number 9: 0
Enter number 10: 7


The largest number is: 10
The smallest number is: -7
Topic archived. No new replies allowed.