Question
Define a single array that may contain either a float value, an integer value, or a character value. Using a menu, enter values from the
Define a single array that may contain either a float value, an integer value, or a character value. Using a menu, enter values from the user to fill an array of maximum size 50 until the user chooses to stop. After all input has been stored, find and print the largest float value, compute and print the average of the integer values, and print the alphabets stored in the array. You may need to use unions and enumerated data types to perform the task. Example run:
1. Enter an integer value 2. Enter a float value 3. Enter a character value 4. Quit Enter the value of your choice: 2 Enter the float value: 3.5 1. Enter an integer value 2. Enter a float value 3. Enter a character value 4. Quit Enter the value of your choice: 3 Enter the character value: ] 1. Enter an integer value 2. Enter a float value 3. Enter a character value 4. Quit Enter the value of your choice: 1 Enter the integer value: 4 1. Enter an integer value 2. Enter a float value 3. Enter a character value 4. Quit Enter the value of your choice: 2 Enter the float value: 1.2 1. Enter an integer value 2. Enter a float value 3. Enter a character value 4. Quit Enter the value of your choice: 7 Wrong choice. Please try again!! 1. Enter an integer value 2. Enter a float value 3. Enter a character value 4. Quit Enter the value of your choice: 1 Enter the integer value: 5 1. Enter an integer value 2. Enter a float value 3. Enter a character value 4. Quit Enter the value of your choice: 3 Enter the character value: w 1. Enter an integer value 2. Enter a float value 3. Enter a character value 4. Quit Enter the value of your choice: 4 The largest float value entered is 3.50 The average of the integer values is 4.50 The alphabets entered: w
=======================================
i do the code as this :
#include
enum valueType {Float=1 ,integer ,charcter};
typedef struct v{
enum valueType whatType;
union kind {
float forF;
int forI;
char forc;
}typee;
}val;
int main ()
{
int i;
val arr[50];
int sum =0 ;
float Lfloat;
int counter =0;
double avrg ;
do{
printf("the Menu ");
printf("1.Enter an float value ");
printf("2.Enter an integer value ");
printf("3.Enter an character value ");
printf("enter your choice from 1 to 4(quiet) ");
scanf("%d",&i );
switch (i){
case 1:
arr[i].whatType=1 ;
printf("enter your float value :");
scanf("%f",&arr[i].typee.forF);
break;
case 2:
arr[i].whatType=2 ;
printf("enter your integer value :");
scanf("%d",&arr[i].typee.forI);
break;
case 3:
arr[i].whatType=3 ;
printf("enter your char value :");
scanf(" %c",&arr[i].typee.forc);
break;
case 4 :
break;
default:
printf("the value you enter should be [0...4] try again : ");
break;
scanf("%d",&arr[i].typee.forI);
}
}while (i != 4);
for(i=0 ;i<50 ; i++){
if(arr[i].whatType == 1){
Lfloat=arr[i].typee.forF;
if(arr[i].typee.forF >Lfloat ){
Lfloat = arr[i].typee.forF;
}
}
if(arr[i].whatType == 2){
sum =+arr[i].typee.forI;
counter ++;
}
}
printf("the largest float value is : %f " , Lfloat);
avrg = sum/counter;
printf(" the avrage of integer value is : %d " , avrg);
printf(" the alphabets entered is " );
for(i=0 ;i<50 ; i++){
if(arr[i].whatType == 3){
printf("%c " ,arr[i].typee.forc );
}
}
return 0 ;
}
My problem is largest float and the avrege i have no idea what wrong with it
it print it as zeroes or uncorrect numbers
other wise my program is working very will
please help me with my program
(in c programming language)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started