Thread: Problem with calculating

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    29

    Problem with calculating

    Hi all i make program for rating movies with matrix, I use 5 movies but calculating marks in not good it's give me some other numbers than real
    Code:
    #include <stdio.h>int main()
    {
        int a[5][5],i,j,g=0;
        float s=1;
        float max=0;
        for(i=0; i<5; i++)
        {
            for(j=0; j<5; j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        i=0;
            for(j=0; j<5; j++)
            {
                s+=a[i][j]/5;
                if(s>max)
                {
                    max=s;
                    g=j;
                }
                printf("Movie %d mark %.2f",j,s);
            }
            printf("Biggest mark have movie number %d, mark: %.2f",g,max);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > s+=a[i][j]/5;
    1. i is fixed at 0, so why did you input all those extra numbers?

    2. a is an array of integers, so divide by 5 will truncate. 8 for example, will simply be 1 rather than 1.6.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    I change array in float but again is not giving me real result

  4. #4
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by John5 View Post
    Hi all i make program for rating movies with matrix
    How?

    We know that you are working with five movies, and ask the user 5×5 numbers.

    What we do not know, is how this matrix should be interpreted. Does each row correspond to a movie, and column to a specific critic? Or something else? Until we know what the matrix represents, and how and what information you wish to extract from it, helping you is pretty much just guesswork. Please, help us help you.

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    Every row is marks just for one movie, i enter 5 marks for every movie

  6. #6
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Do you need the separate marks for each movie, or just the average and the maximum?

  7. #7
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    I just need average mark for every movie and biggest average mark and what movie has biggest average mark.

  8. #8
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Are the movie ratings given in a specific format, on can you require any input format you want?

  9. #9
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    You can only enter marks from 1-10 in every row you have 5 marks and there is 5 movies

  10. #10
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    In that case, why don't you use a loop over each movie, and an inner loop that remembers the number of scores, the sum of those scores, and the maximum score given, for that movie?

    If the scores for the same movie are separated by spaces, movies are separated by commas, and the input ends with a period, you might use something like the following:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    
    #define MAX_MOVIES  50
    
    int main(void)
    {
        int movie_scores[MAX_MOVIES];
        int movie_maxscore[MAX_MOVIES];
        int movie_sumscore[MAX_MOVIES];
        int movies = 0;
    
        fprintf(stderr, "Please supply movie scores [1 - 10] separated by spaces.\n");
        fprintf(stderr, "Separate movies with a comma ','.\n");
        fprintf(stderr, "End the input with a period '.'.\n");
        fflush(stderr);
    
        while (movies < MAX_MOVIES) {
            int  scores = 0;    /* No scores given for this movie */
            int  sum    = 0;    /* Sum of scores given to this movie */
            int  max    = 0;    /* Largest score given to this movie */
            int  s;             /* Current score */
            int  c;             /* Temporary character */
    
            while (scanf("%d", &s) == 1)
                if (s >= 1 && s <= 10) {
                    if (max < s)
                        max = s;
                    sum += s;
                    scores++;
                } else {
                    fprintf(stderr, "Error: Score %d is invalid.\n", s);
                    return EXIT_FAILURE;
                }
    
            if (scores > 0) {
                fprintf(stderr, "Movie %d: %d scores given, highest %d, average %.2f\n",
                                movies + 1, scores, max, (double)sum / (double)scores);
                fflush(stderr);
    
                /* Save into the three movie arrays. */
                movie_scores[movies] = scores;
                movie_maxscore[movies] = max;
                movie_sumscore[movies] = sum;
                movies++;
            }
    
            do {
                c = getc(stdin);
            } while (c != EOF && isspace(c));
    
            /* No more movies? */
            if (c == '.' || c == EOF)
                break;
    
            /* Next movie? */
            if (c == ',')
                continue;
    
            /* Bad input. */
            fprintf(stderr, "Error: Invalid character '%c' in input.\n", c);
            return EXIT_FAILURE;
        }
    
        if (movies < 1) {
            fprintf(stderr, "No movies scored.\n");
            return EXIT_FAILURE;
        }
    
        fprintf(stderr, "Scored %d movies.\n", movies);
        fflush(stderr);
    
        return EXIT_SUCCESS;
    }
    For example, if you input 4 6 7, 2 3 1 4 1 1, 7 8 4 3, 6. it will output
    Please supply movie scores [1 - 10] separated by spaces.
    Separate movies with a comma ','.
    End the input with a period '.'.
    Movie 1: 3 scores given, highest 7, average 5.67
    Movie 2: 6 scores given, highest 4, average 2.00
    Movie 3: 4 scores given, highest 8, average 5.50
    Movie 4: 1 scores given, highest 6, average 6.00
    Scored 4 movies.
    For movie i, 0 <= i and i < movies,
    the number of scores given is movie_scores[i]
    the highest score is movie_maxscore[i]
    the average score is (double)movie_sumscore[i] / (double)movie_scores[i]
    so you can easily add whatever movie comparisons you want.

  11. #11
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    I need to make this program with matrix not just with loops.

  12. #12
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Getting information out from you is like pulling teeth. You could have stated the requirements in your initial post, and saved three days of waffling.

    If you had added the point of the exercise -- that is, what features of the C language this is intended to help you get familiar with --, that would have made the question interesting.

  13. #13
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    I don't know what anything to explain, from code above you can see that i work with matrix, not just with loops.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Quote Originally Posted by John5 View Post
    I need to make this program with matrix not just with loops.
    Why?

    Because it's one of those pointless exercises which is basically akin to teaching you magic tricks, as opposed to teaching you how to program.

    Any sane program would use loops to implement a generic matrix multiply.

    OK, 3x3 is about the limit you would want to go to if you were seeking max speed and could be bothered to write out all the steps by hand.

    But at 5x5, it's looking kinda silly and pointless.
    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.

  15. #15
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    I need this program for school i know that just with loops is more practial but tell that to old school teaching program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-10-2015, 06:57 AM
  2. problem with simpson funcition and calculating
    By ovojan in forum C Programming
    Replies: 2
    Last Post: 06-06-2011, 10:34 AM
  3. noob has a problem calculating ints
    By shroud in forum C Programming
    Replies: 1
    Last Post: 06-26-2010, 12:03 PM
  4. Newbie calculating problem
    By Andy123 in forum C Programming
    Replies: 5
    Last Post: 02-09-2006, 12:57 PM
  5. calculating postfix problem
    By Axolotl in forum C Programming
    Replies: 2
    Last Post: 04-01-2004, 08:49 PM