Skip to content

Instantly share code, notes, and snippets.

@bzdgn
Created November 11, 2015 11:00
Show Gist options
  • Save bzdgn/a1ba7ea0536c9941fcf8 to your computer and use it in GitHub Desktop.
Save bzdgn/a1ba7ea0536c9941fcf8 to your computer and use it in GitHub Desktop.
A simple Grade Array Example
#include <stdio.h>
#include <stdlib.h>
#define MAX_STUDENT 10
#define MAX_GRADE 100
int main(void)
{
int arr[MAX_STUDENT];
int option = -1; /* initial value */
int i;
int index = 0;
int grade = 0;
/* initialization of array */
for(int i = 0; i < MAX_STUDENT; i++)
{
arr[i] = 0;
}
while(1)
{
printf("\n\nWelcome to the ARX gradebook.\n1.Enter Grade, 2.Print One Grade, 3.Print all Grades, 4. Exit.\n\n:");
scanf("%d", &option);
if(option == 1) /* insert/update grade */
{
printf(" Please enter an index < %d\n:", MAX_STUDENT);
scanf("%d", &index);
if ( (index < 0) || (index > MAX_STUDENT) )
{
printf(" illegal index entered!\n");
}
else
{
printf(" Please enter a grade < %d\n:", MAX_GRADE);
scanf("%d", &grade);
if ( (grade < 0) || (grade > MAX_GRADE) )
{
printf(" illegal grade entered!\n");
}
else
{
arr[index] = grade;
printf(" [update success] Student[%d] = %d\n:", index, arr[index]);
}
}
}
if(option == 2) /* Print index grade */
{
printf("...Please enter an index < %d\n:", MAX_STUDENT);
scanf("%d", &index);
if ( (index < 0) || (index > MAX_STUDENT) )
{
printf("...illegal index entered!\n");
}
else
{
printf("...Student[%d] = %d\n", index, arr[index]);
}
}
if(option == 3) /* Print all grades */
{
for(i = 0; i < MAX_STUDENT; i++)
{
printf("Student [%d] = %d\n", i, arr[i]);
}
}
if(option == 4) /* Exit program */
{
exit(0);
}
else
{
printf("\nPlease enter a valid option [1,2,3,4]\n");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment