In C, you are using loops and functions to calculate and display information abo
ID: 3765774 • Letter: I
Question
In C, you are using loops and functions to calculate and display information about test results. The test results should be input by the user and they should be positive decimal values. Please complete the following:
1. Ask the user how many test results he or she would like to enter. Based on this value, write a function that collects that many test results and stores them in specified variables passed into the function.
2. Pass the values by reference into separate functions as requested by the user to do the following:
a. Find average
b. Find min and max
c. Display Highest to Lowest
3. For security purposes, your company validates data frequently. Write a function that displays the values and addresses for each of the variables passed by reference.
4. Write a main function with menu options to allow users to use any of the functions in 1 – 3 including descriptions of what each menu option will do.
5. Configure your input parameters so that once the input is collected, no data is passed in or out of the functions, only reference variables
6. Add comments to detail what and why you are doing what you are doing in each function and in the int main function.
Explanation / Answer
1)
passing of array numbers:
#include <stdio.h>
int maximum( int [] ); //max is a function
int maximum( int values[20] ) // maximum it will take 20 numbers
{
int max_value, i; //uisng fuunction find maximum numberer
max_value = values[0]; //passing the values through array with the loop
for( j = 0; j < 10; ++j )
if( values[i] > max_value )
max_value = values[j];
return max_value;
}
main()
{
int values[5], j, max; //main function
printf("Enter 10 numbers ");
for( j = 0; j < 10; ++j )
scanf("%d", &values[j] );
max = maximum( values );
printf(" Maximum value is %d ", max );
}
3)
passing the variables with adress:
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
void func(struct student *record);
int main()
{
struct student record;
record.id=1;
strcpy(record.name, "AAA");
record.percentage = 90.6;
func(&record);
return 0;
}
void func(struct student *record)
{
printf(" Id is: %d ", record->id);
printf(" Name is: %s ", record->name);
printf(" Percentage is: %f ", record->percentage);
}
5)Menu item:
# include <stdio.h>
int main()
{
char ch; // char for the to check chance by the switch
float num1,num2; //varibles declare
printf("Enter operator either + or - or * or divide : "); //operators
scanf("%c",&o);
printf("Enter two operands: "); //oprands
scanf("%f%f",&num1,&num2);
switch(ch) {
case '+':
printf("%.1f + %.1f = %.1f",num1, num2, num1+num2); //cases for operation
break;
case '-':
printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
break;
case '*':
printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
break;
case '/':
printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
printf("Error! operator is not correct");
break;
}
return 0;
}
5)
passing refference variables;
#include <stdio.h>
struct student{ //student is a structure refference is stu
char name[50];
int roll;
};
void Display(struct student stu);
/* function prototype should be below to the structure declaration otherwise compiler shows error */
int main(){
struct student s1;
printf("Enter student's name: ");
scanf("%s",&s1.name);
printf("Enter roll number:");
scanf("%d",&s1.roll);
Display(s1); // passing structure variable s1 as argument
return 0;
}
void Display(struct student stu){
printf("Output Name: %s",stu.name);
printf(" Roll: %d",stu.roll);
}