Thread: Can't find the infinite loop

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    8

    Can't find the infinite loop

    I'm having an issue finding the infinite loop in my code if someone can help that would be great.




    Code:
    #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 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;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,404
    Use a debugger to set breakpoints and step through your code. From there, you will be able to narrow down where the infinite loop is happening, and hopefully that will be enough insight for you to spot and solve the problem. If not, come back here and tell us what you observed with the help of the debugger.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop in insertNode function is an infinite loop
    By blongnec in forum C Programming
    Replies: 8
    Last Post: 03-19-2016, 09:57 PM
  2. Experts please find the cause of infinite loop
    By sigur47 in forum C Programming
    Replies: 11
    Last Post: 12-11-2011, 09:24 AM
  3. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  4. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM
  5. Infinite #define loop i can't find a way around
    By IonBlade in forum C++ Programming
    Replies: 6
    Last Post: 12-18-2003, 09:38 PM

Tags for this Thread