how do you this?

How would I go about doing this?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

//

#include <iostream>

int main ()
{
	
	
	
	
	
	system("pause");
	return 0;
}
Last edited on

You need an array of type int and a size of 50.

int myArray[50]; : http://www.cplusplus.com/doc/tutorial/arrays/

You need to create a For loop to loop 50 times, asking for a number each time, and storing that number in the array. Hint: you use the For loop variable to point to each index of your array as it loops.

for(int i = 0; i< 50; i++) : http://www.cplusplus.com/doc/tutorial/control/
Last edited on
In addition to what @Softrix suggested, you can also sort the numbers by implementing either bubblesort, insertion sort, or selection sort; or alternatively, you can just use the sort function in the algorithms library:

http://www.cplusplus.com/reference/algorithm/sort/
I cant figure out how to properly Also I want the program to disregard a negative number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>



int main ()
{
	
	
	}
			
			
		
	
		

	system ("pause");
	return 0;
}
Last edited on
closed account (j3Rz8vqX)
You want to report negative when grades[i] is less than 0, not the counter i is greater than 0.

Also, if you print grades[50], you are printing beyond the array by 1.

Possible example of a correctly implemented spoiler for your current available code:
http://pastie.org/9221020#7,11-12,14,17,24

If your code is confidential, you can use the, copy/repaste, button on the top right, editing it to have nothing, to removing its content - ideally (it is public).
Last edited on
Line 15 and 33 are not anywhere close to how you fill and print an array. Maybe in python, you can use list comprehension to fill an entire array with inputs from the user, but in c++ it is a bit different.

To fill the array with inputs from the user, you must make use of a for-loop. What's a for-loop you may ask?
http://www.cplusplus.com/doc/tutorial/control/
Read the section called Iteration statements

As the for-loop iterates, you use the assign the user input to the indices of the array. To understand what is an index, read here:
http://www.cplusplus.com/doc/tutorial/arrays/

To print values from the array, you do the same thing you did above, but this time, you use cout rather than cin.

Lastly, system(...) there are tons of discussions (@Duoas) on this forum as to why you shouldn't use it:
http://www.cplusplus.com/forum/articles/11153/
http://www.cplusplus.com/forum/beginner/1988/
Great thank you!!! and I want to sort the array in ascending order with a sort function, just so I can tidy things up but how?

1
2
3
4
5
6
//librarys i am using
#include <iostream>     
#include <algorithm>

//would this work?
sort (grades.begin(), grades.begin()+4);
Last edited on
closed account (j3Rz8vqX)
Since you are using normal arrays and not vectors, simply call it in a fashion similar to this:
sort (grades, grades+counter);

Where 'counter' is how many elements you've used out of your 50 element array; since we are counting from 0, adding it would be the perfect delimiter.

Sort is iterators to the first element and the last element; and optionally a sort comparison.
Thank you again @Dput ! I have one more question, I want to display the ascending arrayu in reverse.
Last edited on
for(int i = 49; i >= 0; --i)
closed account (j3Rz8vqX)
Well, you'd want to consider 'how much' of the array you were actually using.

In the examples, I provided above, 'counter' was the count variable of how many elements of the array, you were actually using.

So, we would start at the max count-1 and count to 0.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    sort (grades, grades+counter);//Ensures the array is sorted! default!
    cout<<"Reporting after sort: "<<endl;
    for(int i = 0; i< counter; i++)
    {
        //terminates data if negative numbers are entered.
        if(grades[i] < 0)
        {
            invalid = true;
            cout<<("Negative numbers are invalid.")<<endl;
            return 0;
        }
        //Displays Array.
        cout << grades[i] << "\t";
    }
    cout<<"\nReporting in large to small: "<<endl;//Modified print
    for (int i = counter-1; i > -1; --i)//Counter comparison is 'i', not grades
    {
        //Displays Array.
        cout << grades[i] << "\t";
    }
Enter the grades:
5
2
6
3
7
4
1
a
Reporting after sort:
1       2       3       4       5       6       7
Reporting in large to small:
7       6       5       4       3       2       1
Press 'enter' to exit:

That is, if you wanted to print, in reverse order.

You could also achieve the result by sorting in the reverse order before a standard print:
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
//Function declarations:
bool isGreater(int i, int j){return (i>j);}
bool isLess(int i, int j){return (i<j);}

//Inside main:
    sort (grades, grades+counter,isGreater);
    cout<<"Reporting after sort: "<<endl;
    for(int i = 0; i< counter; i++)
    {
        //terminates data if negative numbers are entered.
        if(grades[i] < 0)
        {
            invalid = true;
            cout<<("Negative numbers are invalid.")<<endl;
            return 0;
        }
        //Displays Array.
        cout << grades[i] << "\t";
    }
    cout<<"\nReporting in large to small: "<<endl;//A standard print.
    for (int i = 0; i < counter; ++i)
    {
        //Displays Array.
        cout << grades[i] << "\t";
    }
Enter the grades:
5
2
6
3
7
4
1
a
Reporting after sort:
7       6       5       4       3       2       1
Reporting in large to small:
7       6       5       4       3       2       1
Press 'enter' to exit:


Advice: do not call sort multiple times, simply to change how output is displayed. Manipulating display procedures can be less expensive than forcing a sort and a display; normally.
Last edited on
Topic archived. No new replies allowed.