array

closed account (oj87ko23)
so my assignment is to write a code for arrays. here is what im instructed to do.
Follow these steps to manipulate a one dimensional array:

• Declare a blank array which can hold 10-grades.
• Using a ‘for’ loop enter 10 grades in the array.
• Print the above array.
• Print the array in reverse order.
• Find the average of 10 grades as real number.
• Find the maximum grade.
• Find the minimum grade.
• Arrange and print the array in ascending order.

now I'm able to write the code for everything except the last one, and I'm a little skeptical on find the max and min grade. Also, I'd like to put a pause in between each one, like a 3 second pause that way not everything pops up at once. how do i do that?

here is the code i have so far. I'm sure I have errors, i think im missing brackets in certain places. I'm not great with C++ so I'm sure theres mistakes. I added comments in bold after each different part. Positive and negative feedback is appreciated. I'm sure some of you are thinking "What positive feedback 0_o" lol

int a [10], i, total=0, max=0;
for(i=0; i<=9; i++){
printf ("Enter grades: ");
scanf ("%d", &grades[i]);
printf ("Here are the grades entered: ", grades[i]
}
for(i=0; i<=9; i++){
printf ("%3d",a[i];
}
would the above code print the grades i entered?

for(i=0; i<=9; i++){
total=total+grades[i];
avg=total/10;
printf("Average is: ", avg);
}
this is the code for average of the ten arrays, correct?

if (a[i] >max)
{
max=a[i];
}
else if (a[i] <min}
{
min=a[i];
}
printf("Max number is: %d",max);
printf("Min number is: %d",min);
}
this is the code to print out the max and min numbers out of the 10 numbers in the array, correct?

int hold;
hold=a[0];
a[0]=a[1];
hold=a[1];

this is all my professor gave us for the ascending order part, doesnt seem right,
@jayygerman
int a [10], i, total=0, max=0;
for(i=0; i<=9; i++){
printf ("Enter grades: ");
scanf ("%d", &grades[i]);
printf ("Here are the grades entered: ", grades[i]
}
for(i=0; i<=9; i++){
printf ("%3d",a[i];
}
would the above code print the grades i entered?


In fact each grade is displayed three times. The first time is when you are entering it. The second time is in the first loop with sttatement

printf ("Here are the grades entered: ", grades[i]

and the third time in the second loop

for(i=0; i<=9; i++){
printf ("%3d",a[i];
}

I think that it is too many times when grades are displayed.

Also it would be good to define the corresponding constant

const int N = 10;

and use this constant everywhere in the code. For example (it looks like you are using C89)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const int N = 10;
int a [N];
int total = 0, max = 0;
int  i;
 
for ( i=0; i < N; i++)
{
   printf ( "Enter grade %d: ", i + 1 );
   scanf ( "%d", &grades[i] );
}

printf ( "\nHere are the grades entered: ", );

for ( i = 0; i < N; i++ ) 
{
   printf ( "%d ",a[i] );
} 
 
Last edited on
closed account (oj87ko23)
cool, thank you!
is everything else okay?
Topic archived. No new replies allowed.