How to show input variables from loop etc

This is a discussion on How to show input variables from loop etc within the C Programming forums, part of the General Programming Boards category; So, I'm doing this project asking for number of inputs from user, show the inputs, make a table and average ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    6

    How to show input variables from loop etc

    So, I'm doing this project asking for number of inputs from user, show the inputs, make a table and average it.
    I was only able to go as far as averaging it. I don't know whether the previous ones need array's and need some expert advice on this.
    Here's how it should look like:Name:  fma2-1.png
Views: 373
Size:  62.8 KB
    and here's my code:
    Code:
    #include<stdio.h>
    
    void main()
    {
    int h, size, i;
    float sum, average;
    //double[] p= new double[size]; unsure if needs arrays
    printf("enter number of students");
    scanf("%d",&size);
    
    
    for(i=0,i<size,i++){
    printf("enter height"+(i+1));
    scanf("%d",&h);
    sum+=h
    }
    average=h/size;
    printf("%d"","&average);
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    There are different ways to do it.One easy way would be an array.
    Every time you read one height,store it in the corresponding element of the array.Then print the array(in order to show what you read).Then the average is the sum divided by size,not the h

    PS - i am not expert, but i think this will do what you want.

  3. #3
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    100
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int size, i, sum = 0, *height;
        printf("How many students are in the class?");
        scanf("%d", &size);
        height = malloc(size * sizeof(int));
        for (i = 0; i < size; ++i)
        {
            printf("\nEnter Height of Student %d:", i + 1);
            scanf("%d", &height[i]);
            sum += height[i];
        }
        puts("\nBelow are the entered numbers");
        for(i = 0; i < size; ++i)
        {
            printf("%d\t", height[i]);
        }
        puts("\nBelow is the height statistics of the class\n");
        puts("Height\tnumber of pupils");
        /*
            Implement statistics here
        */
        printf("Average %f\n", (float) sum / size);
        free(height);
        return 0;
    }

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    I won't judge your code.I will judge the fact that you just throw some code with not a single explanative comment...The majority of people will just copy paste this and learn nothing!

  5. #5
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    100
    Quote Originally Posted by std10093 View Post
    I won't judge your code.I will judge the fact that you just throw some code with not a single explanative comment...The majority of people will just copy paste this and learn nothing!
    It wasn't my intention to do all the work for him, just an example how arrays and loops work.

    @sakuraleaf Pay attention to the syntax in some of the lines of your code:
    Code:
    for(i=0,i<size,i++) // you use colons instead of semicolons - the compiler should display a warning
    average=h/size; // the result of dividing two intgerer values is also integer - not floating point value
    printf("%d"","&average); // %d format in printf requires value - not pointer

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    This deffinitely works.
    thanks for the help, DRK it worked out well! It doesn't completely run in TurboC++ but it does work in Code blocks.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    for the statistic thing, how do u think that works?
    is there an actaul code in C that allows that?

  8. #8
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    100
    Quote Originally Posted by sakuraleaf View Post
    It doesn't completely run in TurboC++ but it does work in Code blocks.
    Since you've posted in the C Programming section of the forum, I've written the code to be compliant with the C language.


    Quote Originally Posted by sakuraleaf View Post
    for the statistic thing, how do u think that works? is there an actaul code in C that allows that?
    Define another array that will hold number of students with the same height.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    Quote Originally Posted by DRK View Post
    Define another array that will hold number of students with the same height.
    Okay so i make a new array that holds these values, and sort them out with if statements? I'm not sure with my answer though.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    If you just want to sort an int array, the most easy solution is Bubble_sort.c

    (not the most efficient but this is not a problem for you now)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting user input for structure variables
    By themolejoel in forum C Programming
    Replies: 7
    Last Post: 10-17-2011, 10:49 PM
  2. Replies: 4
    Last Post: 12-29-2010, 10:11 AM
  3. while loop with int variables
    By joeman in forum C++ Programming
    Replies: 1
    Last Post: 02-07-2010, 12:33 PM
  4. user input type variables
    By pastitprogram in forum C++ Programming
    Replies: 1
    Last Post: 09-05-2008, 07:21 AM
  5. C++ Data input & variables - please help
    By giggsy in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2002, 05:38 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21