how do you declare and populate arrays of intergers

hi really need with several pieces of code if you could help me you would be my life saver first code is

Using a for loop to output array values
*/

#include <stdio.h>


int main(void)
{
/* Declare an array of integers */
int Grades[5];
int nCount;

/* Populate the array */
Grades[0] = 98;
Grades[1] = 87;
Grades[2] = 43;
Grades[3] = 67;
Grades[4] = 17;

for(nCount = 0; nCount < 5; nCount++)
{
printf("Array element %d has the value %d\n", nCount, Grades[nCount]);
}

return 0;
}
so on the first one i need to use a for loop to output the arrays , declare the arrays of integers and populate them how do i do that?
You do show some code. What does it do? How does that differ from what your homework should do?

By trying to answer the questions yourself you should learn, which is the purpose of the homework anyway.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main() 
{
	int Grades[5];
	Grades[0] = 98;
	Grades[1] = 87;
	Grades[2] = 43;
	Grades[3] = 67;
	Grades[4] = 17;
	for(int i = 0; i < 5; i++)
	{
		cout<<"Array Element " << i << "has the value = "<<Grades[i]<<"\n";
	}
	return 0;
}



You can declare such array in this manner also
 
int Grades[] = {98,87,43,67,17}; // Array of Size 5 will be crated and value will be same as above. 
Last edited on
closed account (iAk3T05o)
@shravan: 'nCount' is then useless in your code if you are using 'i'
@Nathan2222 : Yeah, thank for pointing out unused variable. I removed the nCount from my code.
Last edited on
Topic archived. No new replies allowed.