Question

In: Computer Science

I'm getting a conversion issue with this program. When I ran this program using the Windows...

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;
}

Solutions

Expert Solution

#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


Related Solutions

This is in Python I am getting an error when I run this program, also I...
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help! #Input Section def main(): name=input("Please enter the customer's name:") age=int(input("Enter age of the customer: ")) number_of_traffic_violations=int(input("Enter the number of traffic violations: ")) if age <=15 and age >= 105: print('Invalid Entry') if number_of_traffic_violations <0: print('Invalid Entry') #Poccessing Section def Insurance(): if age < 25 and number_of_tickets >= 4 and riskCode == 1: insurancePrice = 480 elif age >=...
I'm getting this error: Exception in thread "main" java.lang.NoSuchMethodError: main I tried using public static void...
I'm getting this error: Exception in thread "main" java.lang.NoSuchMethodError: main I tried using public static void main(String[] args){ but that negates all of the methods that I try to write. I'm just trying to make it so that I can enter values. Thanks. Code below: import java.util.Scanner; public class DataSet2 { private double value; private double sum; private int count; public void add(double value){    System.out.println("Enter values, enter -1 to finish");    Scanner scan = new Scanner(System.in);    value =...
I keep getting an error that I cannot figure out with the below VS2019 windows forms...
I keep getting an error that I cannot figure out with the below VS2019 windows forms .net framework windows forms error CS0029 C# Cannot implicitly convert type 'bool' to 'string' It appears to be this that is causing the issue string id; while (id = sr.ReadLine() != null) using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; namespace Dropbox13 { public partial class SearchForm : Form { private List allStudent = new List(); public SearchForm() { InitializeComponent(); } private void SearchForm_Load(object...
I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
In java, I keep getting the error below and I can't figure out what i'm doing...
In java, I keep getting the error below and I can't figure out what i'm doing wrong. Any help would be appreciated. 207: error: not a statement allocationMatrix[i][j];
One part of this question I am getting wrong. I'm assuming it's the test statistic? I...
One part of this question I am getting wrong. I'm assuming it's the test statistic? I got 1 for Question C, and I got 5.432 for question b, and I got B for question C. An undergraduate student performed a survey on the perceived physical and mental health of UBC students for her term project. She collected information by asking students whether they are satisfied with their physical and mental health status. 129 male and 157 female UBC students were...
i keep getting return value ignored and and conversion from double to float , possible data...
i keep getting return value ignored and and conversion from double to float , possible data loss dont know how to fix the mistakes. if you could please fix it. #define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_DEPRECATE #define _CRT_NONSTDC_NO_DEPRECATE #include #include void O_Read_age_1() {    int sum = 0;    int i;    for (i = 1; i <= 2; i++)    {        int temp;        printf("please enter the age of employee %d:", i);        scanf("%d", &temp);       ...
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
I working on this program in C++ and I keep getting 20 errors of the same...
I working on this program in C++ and I keep getting 20 errors of the same type again.cpp:36:11: error: use of undeclared identifier 'Polynomial' int main() { // create a list of polinomials vector<Polynomial> polynomials; // welcome message cout << "Welcome to Polynomial Calculator" << endl; int option = 0; while (option != 6) { // display menu displayMenu(); // get user input; cin >> option; if (option == 1) { cout << "Enter a polynomial :" << endl; string...
Please see if you can correct my code. I am getting an ReferenceError in Windows Powershell...
Please see if you can correct my code. I am getting an ReferenceError in Windows Powershell that says payment is undefined. I am trying to create a main.js file that imports the function from the hr.js file; call the function passing the necessary arguments and log the result to the console. main.js var Dev = require("./hr.js") const { add } = require("./hr.js") var dev_type = 1; var hr = 40; console.log("your weekly payment is " + payment(dev_type, hr)) dev_type =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT