I have a question about an assigtnment im supposed to do.Im supposed to print an array of 20 numbers and then determine the largest and the smallest. Now i have the code:
#include <iostream>
using namespace std;
int main()
{
int myarray[20];
int largest=0;
int smallest=0;
for(int i=0; i<20;i++)
{
cout << "Value for my array [" << i << "]: ";
cin>>myarray[i];
if ( myarray[i] > largest )
{
largest = myarray[i];
}
if (myarray[i]<smallest)
{
smallest=myarray[i];
}
}
for (int i=0; i<20; i++)
{
cout << i << ": " << myArray [ i ] << "\n" ;
}
cout<<"largest number is "<<largest<<endl;
cout<<"Smallest number is "<<smallest<<endl;
return 0;
}
i am able to get the largest number but not the smallest.if anybody know what im doing wrong,id appreciate it.
Or, you can initialize max and min to really small and really large values. Preferably, the largest values of the type:
1 2 3 4
#include <climits>
//...
int max=INT_MIN;
int min=INT_MAX;
Modulo's suggestion, though, is generally viewed as the standard way to get max and min values. Some types (e.g. arbitrary precision numbers) don't have an upper limit, so my method isn't always possible.
im still having trouble.can somebody look at this code and tell me what im doing wrong and what i need to change?
#include <iostream>
using namespace std;
int main()
{
int myarray[20];
int largest;
int smallest;
for(int i=0; i<20;i++)
{
cout << "Value for my array [" << i << "]: ";
cin>>myarray[i];
if ( myarray[i] > largest )
{
largest = myarray[i];
}
if (myarray[i]>smallest)
{
smallest=myarray[i];
}
}
for (int i=0; i<20; i++)
{
cout << i << ": " << myarray [ i ] << "\n" ;
}
cout<<"largest number is "<<largest<<endl;
cout<<"Smallest number is "<<smallest<<endl;
return 0;
}
just a reminder im inputting 20 different numbers into an array and finding the largest and the smallest.
int largest, smallest;
largest = smallest = myarray[0]; // myarray[0] is just garbage, and now so is smallest and largest
Also:
1 2 3 4
if (myarray[i]>smallest) // something's not quite right here but it was right in the first post
{
smallest=myarray[i];
}
Anyway, they mean to accept the first number for the array from the user, then initialize smallest and largest, and then use the loop to accept more numbers and keep smallest and largest up to date.
I know im missing something here:
#include <iostream>
using namespace std;
int main()
{
int myarray[20];
int largest, smallest;
largest=smallest=myarray[0];
for(int i=0; i<20;i++)
{
cout << "Value for my array [" << i << "]: ";
cin>>myarray[i];
if ( myarray[i] > largest )
{
largest = myarray[i];
}
if (myarray[i]>smallest)
{
smallest=myarray[i];
}
}
for (int i=0; i<20; i++)
{
cout << i << ": " << myarray [ i ] << "\n" ;
}
cout<<"largest number is "<<largest<<endl;
cout<<"Smallest number is "<<smallest<<endl;
return 0;
}
please,somebody just tell me what im doing wrong because now i get this:
f:\c++\array\array\array.cpp(12) : warning C4700: uninitialized local variable 'largest' used
1>f:\c++\array\array\array.cpp(16) : warning C4700: uninitialized local variable 'smallest' used
Your code works just fine, but only IF you are going to be entering negative numbers. You initialized smallest as zero, so unless you enter anything less than zero, zero IS the smallest integer. Try something like this:
int smallest = myArray[0];
It will compare everything to the first number that you enter in the array and you won't have to worry about guessing how high or low of an integer will be entered from the user.
Also, you need to make sure all of your variables match up correctly. In your first code, you were using 'myArray' and 'myarray' as the same variable. It's good style choice, when using more than 1 word to declare a variable to cap the first letter of every word after the first word.
You mean myArray[0]...right? (Arrays are 0 based).
Btw, instead of just setting lowest/highest to 0, just set them to the first value that they input (myArray[0]). Or, do the checking after all the values are inside the array.