Present the following menu to the user 1) Print Author Info 2) Enter Fraction 3)
ID: 3707210 • Letter: P
Question
Present the following menu to the user
1) Print Author Info
2) Enter Fraction
3) Print Data
4) Calculate Data
5) Clear Data
0) Exit
Option 1: Print your name, id, and code
Option 2: Ask the user to enter a fraction
Option 3: Print all entered fractions, must print the numeric value and mixed number for, for example 6/4 would print 1 2/4 = 1.5.
Option 4: Sum all entered fractions
Option 5: clear all entered data
Option 0: Exit
You must use the provided skeleton code.
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
void printInfo()
{
}
typedef struct Fraction
{
int num;
int den;
} Fraction;
int enterFractions(Fraction *frac, int count)
{
}
void printFracs(Fraction *fractions, int count)
{
}
double calcualteData(Fraction *fractions, int count, int code)
{
}
int getAuthorCode()
{
}
//Edit above this line, just like the previous exercise, any changes below
//will not be included when grading
int main()
{ int sel;
Fraction fractions[1024];
int count = 0;
int code = getAuthorCode();
while(1) {
printf("1) Print Author Info 2) Enter Fraction 3) Print Data 4) Calculate Data 5) Clear Data 0) Exit ");
scanf("%d", &sel);
if (sel == 0)
{
return(0);
}
else if (sel == 1)
{
printInfo();
}
else if (sel == 2)
{
count = enterFractions(fractions, count);
}
else if (sel == 3)
{
printFracs(fractions, count);
}
else if (sel == 4)
{
printf("Calculated Data: %lf ", calcualteData(fractions, count, code));
}
else if (sel == 5)
{
count = 0;
}
}
return(0);
}
Explanation / Answer
Given below is the code for the question. You need to modify the getAuthorCode() and printInfo() functions to use your own name, id and code.
Please do rate the answer if it was helpful. Thank you
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct Fraction
{
int num;
int den;
} Fraction;
int enterFractions(Fraction *frac, int count)
{
printf("Enter numerator: ");
scanf("%d", &frac[count].num);
printf("Enter denominator: ");
scanf("%d", &frac[count].den);
if(frac[count].den < 0) //move the -ve sign to numerator
frac[count].num = -frac[count].num;
count++;
return count;
}
void printFracs(Fraction *fractions, int count)
{
int i;
int a, b, c;
double val;
for(i = 0; i < count; i++)
{
printf("%d) ", i+1);
a = fractions[i].num / fractions[i].den;
b = fractions[i].num % fractions[i].den;
c = fractions[i].den;
val = ((double)fractions[i].num )/ fractions[i].den;
if(a != 0)
printf("%d %d/%d = %.2f ", a, b, c, val);
else
printf("%d/%d = %.2f ", b, c, val);
}
}
double calcualteData(Fraction *fractions, int count, int code)
{
double sum = 0;
int i = 0;
for(i = 0; i < count; i++)
sum += ((double)fractions[i].num) / fractions[i].den;
return sum;
}
int getAuthorCode()
{
return 0;
}
void printInfo()
{
printf("Name: XXX ");
printf("ID: XXX ");
printf("Code: %d ", getAuthorCode());
}
//Edit above this line, just like the previous exercise, any changes below
//will not be included when grading
int main()
{ int sel;
Fraction fractions[1024];
int count = 0;
int code = getAuthorCode();
while(1) {
printf("1) Print Author Info 2) Enter Fraction 3) Print Data 4) Calculate Data 5) Clear Data 0) Exit ");
scanf("%d", &sel);
if (sel == 0)
{
return(0);
}
else if (sel == 1)
{
printInfo();
}
else if (sel == 2)
{
count = enterFractions(fractions, count);
}
else if (sel == 3)
{
printFracs(fractions, count);
}
else if (sel == 4)
{
printf("Calculated Data: %lf ", calcualteData(fractions, count, code));
}
else if (sel == 5)
{
count = 0;
}
}
return(0);
}
output
=======
1) Print Author Info
2) Enter Fraction
3) Print Data
4) Calculate Data
5) Clear Data
0) Exit
1
Name: XXX
ID: XXX
Code: 0
1) Print Author Info
2) Enter Fraction
3) Print Data
4) Calculate Data
5) Clear Data
0) Exit
2
Enter numerator: 4
Enter denominator: 6
1) Print Author Info
2) Enter Fraction
3) Print Data
4) Calculate Data
5) Clear Data
0) Exit
3
1) 4/6 = 0.67
1) Print Author Info
2) Enter Fraction
3) Print Data
4) Calculate Data
5) Clear Data
0) Exit
2
Enter numerator: 8
Enter denominator: 3
1) Print Author Info
2) Enter Fraction
3) Print Data
4) Calculate Data
5) Clear Data
0) Exit
3
1) 4/6 = 0.67
2) 2 2/3 = 2.67
1) Print Author Info
2) Enter Fraction
3) Print Data
4) Calculate Data
5) Clear Data
0) Exit
4
Calculated Data: 3.333333
1) Print Author Info
2) Enter Fraction
3) Print Data
4) Calculate Data
5) Clear Data
0) Exit
0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.