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

I created a shoppingcart program but I'm getting a few compilation failed errors: 1) Tests that...
I created a shoppingcart program but I'm getting a few compilation failed errors: 1) Tests that ItemToPurchase("Bottled Water", "Deer Park, 12 oz.", 1, 10) correctly initializes item compilation failed 2) Tests default constructor and accessors for ShoppingCart Compilation failed 3)Tests ShoppingCart("John Doe", "February 1, 2016") correctly initializes cart Compilation failed 4) Tests that getNumItemsInCart() returns 6 (ShoppingCart) compilation failed 5) Test that getCostOfCart() returns 9 (ShoppingCart) compilation failed Complete program: itemtopurchase.java public class ItemToPurchase { // instance variables private String...
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...
(C++) I'm getting a few errors that prevents the program from running what is causing this?...
(C++) I'm getting a few errors that prevents the program from running what is causing this? " routes3.cpp:202:9: warning: add explicit braces to avoid dangling else [-Wdangling-else] else { ^ " #include <iostream> #include <vector> #include <string> #include <set> #include <cstring> using namespace std; class Leg{ const char* const startCity; const char* const endCity; const int dist; public: Leg(const char* const, const char* const, int); Leg& operator= (const Leg&); int getDist() const {return dist;} void output(ostream&) const; friend class Route;...
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 need to write this program in Python. Write a program that displays a temperature conversion...
I need to write this program in Python. Write a program that displays a temperature conversion table for degrees Celsius and degrees Fahrenheit. The tabular format of the result should include rows for all temperatures between 70 and 270 degrees Celsius that are multiples of 10 degrees Celsius (check the sample below). Include appropriate headings on your columns. The formula for converting between degrees Celsius and degrees Fahrenheit is as follow F=(9/5C) +32 Celsius Fahrenheit 70 158 80 176 90...
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);       ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT