Thread: Need help with arrays. Can't seem to find my problem in the code.

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    1

    Need help with arrays. Can't seem to find my problem in the code.

    The problem is---
    Must use arrays - Ask the user for ratings for a professor in a loop. The ratings can go from 0 to 100. Allow the user to keep entering ratings, until they type in -1. Store these ratings in an appropriate array.
    Once the user is done entering values, print how how many of each rating a professor has. Show the highest ratings first, and do not show ratings that weren't given. Also print out the total number of ratings and the average.
    Example:
    Enter rating (-1 to quit): 90
    Enter rating (-1 to quit): 89
    Enter rating (-1 to quit): 88
    Enter rating (-1 to quit): -100
    Error..ratings must be between 0 and 100.
    Enter rating (-1 to quit): 101
    Error..ratings must be between 0 and 100.
    Enter rating (-1 to quit): 88
    Enter rating (-1 to quit): -1
    There were 4 ratings entered
    1 rating(s) of 90
    1 rating(s) of 89
    2 rating(s) of 88
    The average of the ratings were: 88.75
    ----------
    My code is
    ------------
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        int rating[100]; // size of 100
        int freq[100]; // size of 100
        int t; // total number of rating
        int size;
        int i = 0, j = 0; // two counters
        int count = 0;
        const int Quit = -1;
        double average;
        double total;
    
    
        /* Input elements in rating */
        printf("Enter rating (-1 to quit): ", i + 1);
        scanf_s("%d", &rating[i]);
    
    
        for (i = 0; i<size; i++)
        {
            scanf("%d", &rating[i]);
    
    
            /* Initially initialize frequencies to -1 */
            freq[i] = -1;
        }
        for (i = 0; i<size; i++)
        {
            count = 1;
            for (j = i + 1; j<size; j++)
            {
                /* If duplicate rating is found */
                if (rating[i] == rating[j])
                {
                    count++;
    
    
                    /* Make sure not to count frequency of same rating again */
                    freq[j] = 0;
                }
            }
    
    
            /* If frequency of current rating is not counted */
            if (freq[i] != 0)
            {
                freq[i] = count;
            }
        }
    
    
        /*
        * Print frequency of each rating
        */
        printf("\nThere were %d ratings entered\n", total amount of ratings);
        for (i = 0; i<size; i++)
        {
            if (freq[i] != 0)
            {
                printf("%d rating(s) of %d \n", freq[i], rating[i]);
            }
        }
    
    
        return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    The problem is that this code isn't yours. We don't do your homework for you.
    Devoted my life to programming...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Read this
    A development process

    Start small, compile often, test every time.

    Now some comments
    Code:
        /* Input elements in rating */
        //!! where is your loop around this code?
        //!! Where do you test for -1 to quit that loop?
        printf("Enter rating (-1 to quit): ", i + 1);
        scanf_s("%d", &rating[i]);
     
        //!! Where did size get a value?
        for (i = 0; i<size; i++)
        {
            //!! Why are you reading ratings AGAIN!?
            //!! Why the change from scanf_s to scanf?
            scanf("%d", &rating[i]);
    
            /* Initially initialize frequencies to -1 */
            //!! Why -1, when later you test for != 0
            freq[i] = -1;
        }
    
        for (i = 0; i<size; i++)
        {
    /// 
            /* If frequency of current rating is not counted */
            if (freq[i] != 0)
    ///
        }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-26-2013, 10:52 AM
  2. Replies: 55
    Last Post: 12-14-2011, 03:58 AM
  3. BIG problem. How do I find a bug in so much code?
    By Yarin in forum C++ Programming
    Replies: 44
    Last Post: 01-31-2008, 12:39 PM
  4. Replies: 12
    Last Post: 06-08-2005, 11:23 AM
  5. can anyone find the problem in my code
    By ArseMan in forum C++ Programming
    Replies: 2
    Last Post: 09-20-2001, 09:02 PM

Tags for this Thread