You will complete this lab using the UMGC MARS Virtual Environment. Specifically, you will use an Ubuntu Linux already configured to include the GNU Compiler Collection (gcc) C compiler and execution software, and text editors including nano, vi and others.
See the First Lab in this Project if you need a review of the compile options for the environment.
Lab Tasks: To successfully complete this lab, you must perform all the following tasks.
Modify the Calculate_Grade Function to use an Array and Pointers
From the previous lab, if we just took the Calculate_Grade function we would have code that looked like this:
Lab Tasks: To successfully complete this lab, you must perform all the following tasks.
Modify the Calculate_Grade Function to use an Array and Pointers
From the previous lab, if we just took the Calculate_Grade function we would have code that looked like this:
/* C Program to Calculate Grade Average */
#include
float Calculate_Grade (float grade1, float grade2, float grade3, float grade4);
int main()
{
float grade1, grade2, grade3, grade4, Grade;
printf("Enter 4 grade values for your course:\n");
scanf("%f%f%f%f",&grade1,&grade2,&grade3,&grade4);
Grade = Calculate_Grade(grade1, grade2, grade3, grade4);
printf("Your grade average is %f\n",Grade);
return 0;
}
float Calculate_Grade(float g1, float g2, float g3, float g4)
float avg;
avg = (g1+g2+g3+g4)/4.0;
return avg;
}
EXAMPLE BELOW:
Recall from our Array and pointer readings, these structures can be used to optimize our code. For example, consider this code found on the https://www.tutorialgateway.org site.
#include
void insertArrayItem(int *parr, int Size)
{
for (int i = 0; i < Size; i++)
{
scanf("%d", parr + i);
}
}
void printArrayItem(int *parr, int Size)
{
for (int i = 0; i < Size; i++)
{
printf("%d ", *(parr + i));
}
}
int main()
{
int Size, i;
printf("Please Enter the size = ");
scanf("%d", &Size);
int arr[Size];
printf("Enter the Elements = ");
insertArrayItem(arr, Size);
printArrayItem(arr, Size);
printf("\n");
}
As you analyze the code, take note of the following:
In the main function, as parameter defining the size of the array is requested. This helps keep our array in bounds.