Array indexing
I have an array with values ranging from 0 - 100
I need to output these results in this format:
>0 :
<=9
<=19
<=29
....
<=99
I know I can just cout << ">0";
and make a for loop going through my array to find value less than 0
and then do another cout <<"<=9;
but this method seems very expensive and not that efficient.
Is there a way I can do this all in one for loop or a nested for loop?
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
|
# include <iostream>
# include <cstdlib>
using namespace std;
int main()
{
int myArray[10],tmpNbr;
for(int i=0;i<10;i++)
{
myArray[i] = rand()%99;
}
for(int i=0;i<9;i++)
{
for(int j=i;j<10;j++)
{
if(myArray[i]>myArray[j])
{
tmpNbr = myArray[i];
myArray[i] = myArray[j];
myArray[j] = tmpNbr;
}
}
}
cout << ">" << myArray[0] << " :" << endl;
for(int i=1;i<10;i++)
{
cout << "<=" << myArray[i] << endl;
}
return 0;
}
|
Hope that helps
Topic archived. No new replies allowed.