In: Computer Science
I'm getting a conversion issue with this program. When I ran this program using the Windows command prompt it compiles and runs without issues. Although, when I run it using the zyBooks compiler it keeps giving me these conversion errors in the following lines:
main.c: In function ‘main’:
main.c:53:24: error: conversion to ‘float’ from ‘long int’ may alter its value [-Werror=conversion] averageCpi += cpi[i] * instructionCount[i] * Million;
main.c:57:29: error: conversion to ‘float’ from ‘long int’ may alter its value [-Werror=conversion] averageCpi = averageCpi / (totalInstructions * Million);
main.c:58:47: error: conversion to ‘float’ from ‘long int’ may alter its value [-Werror=conversion] mips = (frequency * Million) / (averageCpi * Million);
main.c:58:37: error: conversion to ‘float’ from ‘long int’ may alter its value [-Werror=conversion] mips = (frequency * Million) / (averageCpi * Million);
main.c:59:32: error: conversion to ‘float’ from ‘long int’ may alter its value [-Werror=conversion] executionTime = ((averageCpi * (totalInstructions * Million)) / (frequency * Million)) * 1000;
main.c:59:65: error: conversion to ‘float’ from ‘long int’ may alter its value [-Werror=conversion] executionTime = ((averageCpi * (totalInstructions * Million)) / (frequency * Million)) * 1000;
cc1: all warnings being treated as errors
Is there a more efficient way to convert from float to long int?
Here is the program:
#include <stdio.h>
int main() {
int instructionCount[100], totalInstructions = 0, cpi[100], noOfClass = 0, frequency = 0, ch = 0;
float averageCpi = 0 , executionTime , mips;
const long MILLION = 1000000;
do{
printf("\n Performance Assessment: ");
printf("\n ----------- -----------");
printf("\n 1) Enter Parameters");
printf("\n 2) Print Results");
printf("\n 3) Quit");
printf("\n Enter Selection: ");
scanf("%d",&ch);
if(ch == 1){
printf("\n Enter the number of instruction classes: ");
scanf("%d",&noOfClass);
printf("\n Enter the frequency of the machine (MHz): ");
scanf("%d",&frequency);
for (int i = 0; i < noOfClass; ++i) {
printf("\n Enter CPI of class %d : ",(i+1));
scanf("%d", &cpi[i]);
printf("\n Enter instruction count of class %d (millions): ",(i+1));
scanf("%d", &instructionCount[i]);
totalInstructions += instructionCount[i];
}
} else if(ch == 2){
printf("\nFREQUENCY (MHz): %d", frequency);
printf("\nINSTRUCTION DISTRIBUTION");
printf("\nCLASS \t CPI \t COUNT");
for (int i = 0; i < noOfClass; ++i) {
printf("\n %d \t %d \t %d",(i+1), cpi[i], instructionCount[i]);
averageCpi += (cpi[i] * instructionCount[i] * MILLION);
}
averageCpi = averageCpi / (totalInstructions * MILLION);
mips = (frequency * MILLION) / (averageCpi * MILLION);
executionTime = ((averageCpi * (totalInstructions * MILLION)) / (frequency * MILLION)) * 1000;
printf("\n PERFORMANCE VALUES");
printf("\n AVERAGE CPI \t %.2f", averageCpi);
printf("\n TIME (ms) \t %.2f", executionTime );
printf("\n MIPS \t %.2f", mips);
printf("\n");
}
}while(ch != 3);
return 0;
}
#include <stdio.h>
int main() {
int instructionCount[100], totalInstructions = 0, cpi[100], noOfClass = 0, frequency = 0, ch = 0;
float averageCpi = 0 , executionTime , mips;
const long MILLION = 1000000;
do{
printf("\n Performance Assessment: ");
printf("\n ----------- -----------");
printf("\n 1) Enter Parameters");
printf("\n 2) Print Results");
printf("\n 3) Quit");
printf("\n Enter Selection: ");
scanf("%d",&ch);
if(ch == 1){
printf("\n Enter the number of instruction classes: ");
scanf("%d",&noOfClass);
printf("\n Enter the frequency of the machine (MHz): ");
scanf("%d",&frequency);
for (int i = 0; i < noOfClass; ++i) {
printf("\n Enter CPI of class %d : ",(i+1));
scanf("%d", &cpi[i]);
printf("\n Enter instruction count of class %d (millions): ",(i+1));
scanf("%d", &instructionCount[i]);
totalInstructions += instructionCount[i];
}
} else if(ch == 2){
printf("\nFREQUENCY (MHz): %d", frequency);
printf("\nINSTRUCTION DISTRIBUTION");
printf("\nCLASS \t CPI \t COUNT");
for (int i = 0; i < noOfClass; ++i) {
//performing the type conversion to float so that we will no get errors
printf("\n %d \t %d \t %d",(i+1), cpi[i], instructionCount[i]);
averageCpi =(float)averageCpi+ (cpi[i] * instructionCount[i] * MILLION);
}
//performing the type conversion to float so that we will no get errors
averageCpi = (float)averageCpi / (totalInstructions * MILLION);
mips = (float)(frequency * MILLION) / (averageCpi * MILLION);
executionTime = (float)((averageCpi * (totalInstructions * MILLION)) / (frequency * MILLION)) * 1000;
printf("\n PERFORMANCE VALUES");
printf("\n AVERAGE CPI \t %.2f", averageCpi);
printf("\n TIME (ms) \t %.2f", executionTime );
printf("\n MIPS \t %.2f", mips);
printf("\n");
}
}while(ch != 3);
return 0;
}

Note : If you like my answer please rate and help me it is very Imp for me