Thread: c program printing out garbage (arrays and for loops)

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    17

    c program printing out garbage (arrays and for loops)

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    int main ()
    { /* main */
    
    
    
    
        int initial_sum          =  0;
        int first_element        =  0;
        const int min_array_length     =  1;
        const int program_failure_code = -1;
        const int program_success_code =  0;
        int number_of_means      =  2;
        int harmonic_mean_one    =  1;
        float sum_left;
        float sum_right;
        float arithmetic_mean_left;
        float arithmetic_mean_right;
        float arithmetic_mean_both;
        float root_mean_square_left;
        float root_mean_square_right;
        float root_mean_square_both;
        float harmonic_mean_left;
        float harmonic_mean_right;
        float harmonic_mean_both;
        int* left_list = (int*)NULL;
        int* right_list = (int*)NULL;
        int  list_length;
        int  element;
    
    
    /* INPUT */
    
    
    /* list length */
        printf("What is the array length?\n");
        scanf("%d", &list_length);
    
    
        if (list_length < min_array_length) {
            printf("That input is not a valid array length! %d < %d\n",
                   list_length, min_array_length);
            exit(program_failure_code);
        } /* if (list_length < min_array_length) */
    
    
    /* left list */
    
    
        left_list = (int*)malloc(sizeof(int) * list_length);
        if (left_list == (int*)NULL) {
            printf("ERROR: can't allocate an int array of length %d.\n", 
                   list_length);
            exit(program_failure_code);
        } /* if (left_list == (int*)NULL) */
    
    
        for (element = first_element; element < list_length; element++) {
            scanf("%d", &left_list[element]);
        } /* for element */
    
    
    /* right list */
    
    
        right_list = (int*)malloc(sizeof(int) * list_length);
        if (left_list == (int*)NULL) {
            printf("ERROR: can't allocate an int array of length %d.\n", 
                   list_length);
            exit(program_failure_code);
        } /* if (right_list == (int*)NULL) */
    
    
        for (element = first_element; element < list_length; element++) {
            scanf("%d", &right_list[element]);
        } /* for element */
    
    
    
    
    
    
        
    
    
    /* CALC */
    
    
    /* arithmetic mean left list */
    
    
        sum_left = initial_sum;
        for (element = first_element; element < list_length; element++) {
            sum_left = sum_left + left_list[element];
        } /* for element */
    
    
        arithmetic_mean_left = sum_left / list_length;
       
    
    
    /* arithmetic mean right list */
    
    
        sum_right = initial_sum;
        for (element = first_element; element < list_length; element++) {
            sum_right = sum_right + right_list[element];
        } /* for element */
    
    
        arithmetic_mean_right = sum_right / list_length;
    
    
    /* root mean square left list */
    
    
        root_mean_square_left = sqrt(pow(sum_left, 2)) / 
            (list_length);
    
    
    /* root mean square right list */
    
    
        root_mean_square_right = sqrt(pow(sum_right, 2)) / 
            (list_length);
     
    /* harmonic mean left list */
    
    
        harmonic_mean_left = (list_length) / (sum_left);
    
    
    /* harmonic mean right list */
    
    
        harmonic_mean_right = (list_length) / (sum_right);
    
    
    /* arithmetic mean of the arithmetic means */
    
    
        arithmetic_mean_both = ((arithmetic_mean_left + 
                arithmetic_mean_right) / (number_of_means));
    
    
    /* root mean square of the root mean squares */
    
    
        root_mean_square_both = sqrt((pow(root_mean_square_left, 2)) + 
          (pow(root_mean_square_right, 2)) / number_of_means);
    
    
    /* harmonic mean of the harmonic means */
    
    
        harmonic_mean_both = (number_of_means) / ( ((harmonic_mean_one) / 
    (harmonic_mean_left)) + ((harmonic_mean_one) / (harmonic_mean_right)) ); 
    
    
    /* OUTPUT */
    
    
        printf("The %f values are:\n", list_length);
    
    
        printf("The %f values are:\n", list_length);
      
        printf("The arithmetic mean of the left list %f values is %f.\n",
               list_length, arithmetic_mean_left);
        printf("The arithmetic mean of the right list %f values is %f.\n", 
               list_length, arithmetic_mean_right);
        printf("The root mean square of the left list %f values is %f.\n", 
               list_length, root_mean_square_left);
        printf("The root mean square of the right list %f values is %f.\n", 
               list_length, root_mean_square_right);
        printf("The harmonic mean of the left list %f values is %f.\n", 
               list_length, harmonic_mean_left);
        printf("The harmonic mean of the right list %f values is %f.\n", 
               list_length, harmonic_mean_left);
        printf("The arithmetic mean of the arithmetic means is %f.\n", 
               arithmetic_mean_both);
        printf("The root mean square of the root mean squares is %f.\n", 
               root_mean_square_both);
        printf("The harmonic mean of the harmonic means is %f.\n", 
               harmonic_mean_both);
    
    
        free(left_list);
        left_list = (int*)NULL;
        free(right_list);
        right_list = (int*)NULL;
    
    
    
    
    
    
    } /* main */

    The program it supposed to use a data file to input but for now i have a prompt so you can put in a simple one to atleast test it easily. It keeps printing out zeros and garbage. Any help?
    thanks!

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    left_list = (int*)malloc(sizeof(int) * list_length);
    
    //Don't cast malloc. This should be something more like
    left_list = malloc(sizeof(left_list) * list_length);
    Also don't cast NULL.

    What part of the program do the zeros and garbage print?
    Fact - Beethoven wrote his first symphony in C

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Use %f for float values only, never for int. (Use %d for int.)

    Many of your variable names are a lie (the "root mean square" is not a root mean square, nor are the "harmonic mean" variables computing a harmonic mean).

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    17
    What is the array length?
    3
    4 5
    4 5
    4 5
    The 3 values are:
    The arithmetic mean of the left list 3 values is 1610612736.
    The arithmetic mean of the right list 3 values is -1610612736.
    The root mean square of the left list 3 values is 1610612736.
    The root mean square of the right list 3 values is -1610612736.
    The harmonic mean of the left list 3 values is -1610612736.
    The harmonic mean of the right list 3 values is -1610612736.
    The arithmetic mean of the arithmetic means is 0.
    The root mean square of the root mean squares is -1073741824.
    The harmonic mean of the harmonic means is 536870912.


    thats the output with what you said to change

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    17
    So are you saying my calculations aren't correct? What do you suggest I change? Because I am at a total loss here I have no clue what is wrong.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want to calculate the root mean square, then you are not particularly close. The root mean square is, when expanded, the root of the mean of the squares, so you would need to find the arithmetic mean of the squares of the values in the list and then find the root.

    The harmonic mean is the reciprocal of the mean of the reciprocals, so you would need to find the arithmetic mean of the reciprocals and then find the reciprocal of that number.

  7. #7
    Registered User
    Join Date
    Nov 2013
    Posts
    17
    Ohhhhhhh I think I see what you mean, i'll fix that. Thanks!!! I hope that's the problem!

  8. #8
    Registered User
    Join Date
    Nov 2013
    Posts
    17
    okay there must be some other problem because the arithmetic mean isn't even working even though im pretty sure its right...

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Post your latest code.
    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.

  10. #10
    Registered User
    Join Date
    Nov 2013
    Posts
    17
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    int main ()
    { /* main */
    
    
    
    
        const int first_element        =  0;
        const int min_array_length     =  1;
        const int program_failure_code = -1;
        const int program_success_code =  0;
        int number_of_means      =  2;
        int harmonic_mean_one    =  1;
        int initial_sum = 0;
        float sum_left;
        float sum_right;
        float arithmetic_mean_left;
        float arithmetic_mean_right;
        float arithmetic_mean_both;
        float root_mean_square_left;
        float root_mean_square_right;
        float root_mean_square_both;
        float harmonic_mean_left;
        float harmonic_mean_right;
        float harmonic_mean_both;
        int* left_list = (int*)NULL;
        int* right_list = (int*)NULL;
        int  list_length;
        int  element;
    
    
    /* INPUT */
    
    
    /* list length */
        printf("What is the array length?\n");
        scanf("%d", &list_length);
    
    
        if (list_length < min_array_length) {
            printf("That input is not a valid array length! %d < %d\n",
                   list_length, min_array_length);
            exit(program_failure_code);
        } /* if (list_length < min_array_length) */
    
    
    /* left list */
    
    
        left_list = (int*)malloc(sizeof(int) * list_length);
        if (left_list == (int*)NULL) {
            printf("ERROR: can't allocate an int array of length %d.\n", 
                   list_length);
            exit(program_failure_code);
        } /* if (left_list == (int*)NULL) */
    
    
        for (element = first_element; element < list_length; element++) {
            scanf("%d", &left_list[element]);
        } /* for element */
    
    
    /* right list */
    
    
        right_list = (int*)malloc(sizeof(int) * list_length);
        if (left_list == (int*)NULL) {
            printf("ERROR: can't allocate an int array of length %d.\n", 
                   list_length);
            exit(program_failure_code);
        } /* if (right_list == (int*)NULL) */
    
    
        for (element = first_element; element < list_length; element++) {
            scanf("%d", &right_list[element]);
        } /* for element */
    
    
    
    
    
    
        
    
    
    /* CALC */
    
    
    /* arithmetic mean left list */
    
    
        sum_left = initial_sum * 1.0;
        for (element = first_element; element < list_length; element++) {
            sum_left = sum_left + left_list[element];
        } /* for element */
    
    
        arithmetic_mean_left = sum_left / list_length;
       
    
    
    /* arithmetic mean right list */
    
    
        sum_right = initial_sum * 1.0;
        for (element = first_element; element < list_length; element++) {
            sum_right = sum_right + right_list[element];
        } /* for element */
    
    
        arithmetic_mean_right = sum_right / list_length;
    
    
    /* root mean square left list */
    
    
        root_mean_square_left = sqrt((pow(sum_left, 2)) / 
            (list_length));
    
    
    /* root mean square right list */
    
    
        root_mean_square_right = sqrt((pow(sum_right, 2)) / 
            (list_length));
     
    /* harmonic mean left list */
    
    
        harmonic_mean_left = (list_length) / (sum_left);
    
    
    /* harmonic mean right list */
    
    
        harmonic_mean_right = (list_length) / (sum_right);
    
    
    /* arithmetic mean of the arithmetic means */
    
    
        arithmetic_mean_both = ((arithmetic_mean_left + 
                arithmetic_mean_right) / (number_of_means));
    
    
    /* root mean square of the root mean squares */
    
    
        root_mean_square_both = sqrt((pow(root_mean_square_left, 2)) + 
          (pow(root_mean_square_right, 2)) / number_of_means);
    
    
    /* harmonic mean of the harmonic means */
    
    
        harmonic_mean_both = (number_of_means) / ( ((harmonic_mean_one) / 
          (harmonic_mean_left)) + ((harmonic_mean_one) / 
          (harmonic_mean_right)) ); 
    
    
    /* OUTPUT */
    
    
        printf("The %d values are:\n", list_length);
    
    
      
        printf("The arithmetic mean of the left list %d values is %d.\n",
               list_length, arithmetic_mean_left);
        printf("The arithmetic mean of the right list %d values is %d.\n", 
               list_length, arithmetic_mean_right);
        printf("The root mean square of the left list %d values is %d.\n", 
               list_length, root_mean_square_left);
        printf("The root mean square of the right list %d values is %d.\n", 
               list_length, root_mean_square_right);
        printf("The harmonic mean of the left list %d values is %d.\n", 
               list_length, harmonic_mean_left);
        printf("The harmonic mean of the right list %d values is %d.\n", 
               list_length, harmonic_mean_left);
        printf("The arithmetic mean of the arithmetic means is %d.\n", 
               arithmetic_mean_both);
        printf("The root mean square of the root mean squares is %d.\n", 
               root_mean_square_both);
        printf("The harmonic mean of the harmonic means is %d.\n", 
               harmonic_mean_both);
    
    
        free(left_list);
        left_list = (int*)NULL;
        free(right_list);
        right_list = (int*)NULL;
    
    
    
    
    
    
    } /* main */
    I realize harmonic mean isn't correct (i guess?) but im pretty sure all the other calculations are correct.

    If you know how to type these calculations please show me!

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    %d for int.

    %f for float.

    Your compiler is telling you this if you listen.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Check your formats.
    Code:
    $ gcc -Wall -Wextra foo.c
    foo.c: In function ‘main’:
    foo.c:173:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘double’ [-Wformat]
    foo.c:175:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘double’ [-Wformat]
    foo.c:177:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘double’ [-Wformat]
    foo.c:179:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘double’ [-Wformat]
    foo.c:181:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘double’ [-Wformat]
    foo.c:183:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘double’ [-Wformat]
    foo.c:185:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:187:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:189:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:15:15: warning: unused variable ‘program_success_code’ [-Wunused-variable]
    foo.c:202:1: warning: control reaches end of non-void function [-Wreturn-type]
    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.

  13. #13
    Registered User
    Join Date
    Nov 2013
    Posts
    17
    okay fixed the %f's and getting more reasonable answers but the calculations still aren't working out correctly


    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    int main ()
    { /* main */
    
    
    
    
        const int first_element        =  0;
        const int min_array_length     =  1;
        const int program_failure_code = -1;
        const int program_success_code =  0;
        int number_of_means      =  2;
        int harmonic_mean_one    =  1;
        int initial_sum = 0;
        float sum_left;
        float sum_right;
        float arithmetic_mean_left;
        float arithmetic_mean_right;
        float arithmetic_mean_both;
        float root_mean_square_left;
        float root_mean_square_right;
        float root_mean_square_both;
        float harmonic_mean_left;
        float harmonic_mean_right;
        float harmonic_mean_both;
        int* left_list = (int*)NULL;
        int* right_list = (int*)NULL;
        int  list_length;
        int  element;
    
    
    /* INPUT */
    
    
    /* list length */
        printf("What is the array length?\n");
        scanf("%d", &list_length);
    
    
        if (list_length < min_array_length) {
            printf("That input is not a valid array length! %d < %d\n",
                   list_length, min_array_length);
            exit(program_failure_code);
        } /* if (list_length < min_array_length) */
    
    
    /* left list */
    
    
        left_list = (int*)malloc(sizeof(int) * list_length);
        if (left_list == (int*)NULL) {
            printf("ERROR: can't allocate an int array of length %d.\n", 
                   list_length);
            exit(program_failure_code);
        } /* if (left_list == (int*)NULL) */
    
    
        for (element = first_element; element < list_length; element++) {
            scanf("%d", &left_list[element]);
        } /* for element */
    
    
    /* right list */
    
    
        right_list = (int*)malloc(sizeof(int) * list_length);
        if (left_list == (int*)NULL) {
            printf("ERROR: can't allocate an int array of length %d.\n", 
                   list_length);
            exit(program_failure_code);
        } /* if (right_list == (int*)NULL) */
    
    
        for (element = first_element; element < list_length; element++) {
            scanf("%d", &right_list[element]);
        } /* for element */
    
    
    
    
    
    
        
    
    
    /* CALC */
    
    
    /* arithmetic mean left list */
    
    
        sum_left = initial_sum * 1.0;
        for (element = first_element; element < list_length; element++) {
            sum_left = sum_left + left_list[element];
        } /* for element */
    
    
        arithmetic_mean_left = sum_left / list_length;
       
    
    
    /* arithmetic mean right list */
    
    
        sum_right = initial_sum * 1.0;
        for (element = first_element; element < list_length; element++) {
            sum_right = sum_right + right_list[element];
        } /* for element */
    
    
        arithmetic_mean_right = sum_right / list_length;
    
    
    /* root mean square left list */
    
    
        root_mean_square_left = sqrt((pow(sum_left, 2)) / 
            (list_length));
    
    
    /* root mean square right list */
    
    
        root_mean_square_right = sqrt((pow(sum_right, 2)) / 
            (list_length));
     
    /* harmonic mean left list */
    
    
        harmonic_mean_left = (list_length) / (sum_left);
    
    
    /* harmonic mean right list */
    
    
        harmonic_mean_right = (list_length) / (sum_right);
    
    
    /* arithmetic mean of the arithmetic means */
    
    
        arithmetic_mean_both = ((arithmetic_mean_left + 
                arithmetic_mean_right) / (number_of_means));
    
    
    /* root mean square of the root mean squares */
    
    
        root_mean_square_both = sqrt((pow(root_mean_square_left, 2)) + 
          (pow(root_mean_square_right, 2)) / number_of_means);
    
    
    /* harmonic mean of the harmonic means */
    
    
        harmonic_mean_both = (number_of_means) / ( ((harmonic_mean_one) / 
          (harmonic_mean_left)) + ((harmonic_mean_one) / 
          (harmonic_mean_right)) ); 
    
    
    /* OUTPUT */
    
    
        printf("The %d values are:\n", list_length);
    
    
      
        printf("The arithmetic mean of the left list %d values is %f.\n",
               list_length, arithmetic_mean_left);
        printf("The arithmetic mean of the right list %d values is %f.\n", 
               list_length, arithmetic_mean_right);
        printf("The root mean square of the left list %d values is %f.\n", 
               list_length, root_mean_square_left);
        printf("The root mean square of the right list %d values is %f.\n", 
               list_length, root_mean_square_right);
        printf("The harmonic mean of the left list %d values is %f.\n", 
               list_length, harmonic_mean_left);
        printf("The harmonic mean of the right list %d values is %f.\n", 
               list_length, harmonic_mean_left);
        printf("The arithmetic mean of the arithmetic means is %f.\n", 
               arithmetic_mean_both);
        printf("The root mean square of the root mean squares is %f.\n", 
               root_mean_square_both);
        printf("The harmonic mean of the harmonic means is %f.\n", 
               harmonic_mean_both);
    
    
        free(left_list);
        left_list = (int*)NULL;
        free(right_list);
        right_list = (int*)NULL;
    
    
    
    
    
    
    } /* main */

  14. #14
    Registered User
    Join Date
    Nov 2013
    Posts
    17
    can any of yall tell me how to type the correct calculations?

  15. #15
    Registered User
    Join Date
    Nov 2013
    Posts
    17
    What is the array length?
    3
    4 5
    4 5
    4 5
    The 3 values are:
    The arithmetic mean of the left list 3 values is 4.333333.
    The arithmetic mean of the right list 3 values is 4.666667.
    The root mean square of the left list 3 values is 7.505554.
    The root mean square of the right list 3 values is 8.082904.
    The harmonic mean of the left list 3 values is 0.230769.
    The harmonic mean of the right list 3 values is 0.230769.
    The arithmetic mean of the arithmetic means is 4.500000.
    The root mean square of the root mean squares is 9.433981.
    The harmonic mean of the harmonic means is 0.222222.


    thats the most recent output

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with arrays and printing them in for loops....
    By Alex Coven in forum C Programming
    Replies: 15
    Last Post: 10-17-2013, 07:14 PM
  2. Replies: 2
    Last Post: 07-21-2013, 10:35 PM
  3. Replies: 5
    Last Post: 07-09-2012, 08:18 AM
  4. Loops keep printing 0
    By kaopei in forum C Programming
    Replies: 15
    Last Post: 12-03-2010, 10:17 AM
  5. Structs - garbage printing
    By MethodMan in forum C Programming
    Replies: 11
    Last Post: 03-14-2004, 10:48 AM