C programming language help, I have displayed my code below. The program display
ID: 3792354 • Letter: C
Question
C programming language help, I have displayed my code below. The program displays a menu and if 1 is selected, the user then inputs some parameters. The menu is then displayed again. If 2 is selected, the program should display the users parameters in a formatted table. However, I am having trouble getting the users input to print it out in a formatted table.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int numOfInst, totalCycles, totalInst, clockRate;
void parameters(){
totalInst = 0;
totalCycles = 0;
int counter, cpiClass, instCount;
printf("Enter the number of instruction classes: ");
scanf(" %d", &numOfInst);
printf("Enter the frequency of the machine (MHz): ");
scanf(" %d", &clockRate);
for (counter = 0; counter < numOfInst; counter++){
printf("Enter CPI of class %d: ", counter + 1);
scanf(" %d", &cpiClass);
printf("Enter instruction count of class %d (millions): ", counter + 1);
scanf(" %d", &instCount);
totalInst += instCount;
totalCycles += cpiClass * instCount;
}
return;
}
float avgCPI(){
float avg = totalCycles / ((float)totalInst);
return (avg);
}
float execTime(){
float time = (totalCycles / ((float)clockRate)) * 1000;
return (time);
}
float calcMips(){
float mips = totalInst / (totalCycles / ((float)clockRate));
return (mips);
}
void printParam(){
int i;
printf("------------------------- ");
printf("| Class | CPI |Count | ");
for (i = 0; i < numOfInst; i++);
printf(" %d ", (i+1));
/*totalCycles[i], totalInst[i]);*/
return;
}
/*void printPerformance{
}*/
int main()
{
int option;
do {
printf("Performance assessment: ");
printf(" -----------------------");
printf(" 1) Enter parameters: ");
printf(" 2) Print table of parameters: ");
printf(" 3) Print table of performance ");
printf(" 4) Quit ");
scanf(" %d", &option);
switch (option){
case 1: parameters();
break;
case 2: printParam();
break;
case 3:
break;
case 4:
break;
default: printf("Invalid input, please enter a number from 1-4 ");
}
}while(option != 4);
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int numOfInst, totalCycles, totalInst, clockRate;
void parameters(){
totalInst = 0;
totalCycles = 0;
int counter, cpiClass, instCount;
printf("Enter the number of instruction classes: ");
scanf(" %d", &numOfInst);
printf("Enter the frequency of the machine (MHz): ");
scanf(" %d", &clockRate);
for (counter = 0; counter < numOfInst; counter++){
printf("Enter CPI of class %d: ", counter + 1);
scanf(" %d", &cpiClass);
printf("Enter instruction count of class %d (millions): ", counter + 1);
scanf(" %d", &instCount);
totalInst += instCount;
totalCycles += cpiClass * instCount;
}
return;
}
float avgCPI(){
float avg = totalCycles / ((float)totalInst);
return (avg);
}
float execTime(){
float time = (totalCycles / ((float)clockRate)) * 1000;
return (time);
}
float calcMips(){
float mips = totalInst / (totalCycles / ((float)clockRate));
return (mips);
}
void printParam(){
int i;
printf("| Class | CPI | Count | ");
printf(" ----------------------------------------------- ");
for (i = 0; i < numOfInst; i++);
printf(" %d %d %d ",(i+1), totalCycles, totalInst);
}
/*void printPerformance{
}*/
int main()
{
int option;
do {
printf(" 1) Enter parameters: ");
printf(" 2) Print table of parameters: ");
printf(" 3) Print table of performance ");
printf(" 4) Quit ");
scanf(" %d", &option);
switch (option){
case 1: parameters();
break;
case 2: printParam();
break;
case 3:
break;
case 4:
break;
default: printf("Invalid input, please enter a number from 1-4 ");
}
}while(option != 4);
return 0;
}
output
1) Enter parameters:
2) Print table of parameters:
3) Print table of performance
4) Quit
2
| Class | CPI | Count |
-----------------------------------------------
26 6921 314
1) Enter parameters:
2) Print table of parameters:
3) Print table of performance
4) Quit
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.