help

This is a discussion on help within the C Programming forums, part of the General Programming Boards category; My school assigment on c includes writing a program that lets u input as many numbers as u want and ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    17

    help

    My school assigment on c includes writing a program that lets u input as many numbers as u want and when u press "0" it stops and then it counts the average of them and also tells u which was the smallest number. Other stuff i can do, but i cant figure out how to print the smallest number that was given. at the moment as u can see in the code, what is supposed to be the smallest number is the first index of the array. please help, im due tommorrow, thank u for ur time.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #define SIZE 127
    
    int main()
    {
        int lkm=0;                         
        int i=1;
        int array[127]={0};
        int total=0;
        
        do
        {
            printf("give number %d.", i);
            scanf("%d", &array[i]);
            lkm++;
            i++;
        }while(array[i-1]>0);
            
        lkm--;                             
        
        for(i=1; i<=lkm; i++)
        {
                 total+=array[i];
        }
        printf("numbers average is %d and the smallest number was %d", total/lkm, array[1]);
    getch();
    }

  2. #2
    Registered User
    Join Date
    Jul 2009
    Posts
    51
    First, you #define SIZE, but then hard code the value into your integer array later. Any reason?

    As for getting the lowest value. Perhaps adding a variable and assigning the lowest value to that would work(either as you're entering numbers, or after by iterating through the array).

    Also, don't wait until the last minute to do your homework, it'll give you ulcers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    First element is the smallest will work only if the array is sorted.
    Besides in the C language array indexing starts at 0 not 1.
    Create another variable to keep track of the smallest element.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    17
    No reason for #define, i should delete it, it was definition of the size of the array. Ok thanks for the advice.

    P.s. I didnt leave things to the last minute, they streched to it!

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    17
    Can u sort arrays?? that would be great, then i would just have to assign array[0] for the smallest number.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Fenix View Post
    Can u sort arrays?? that would be great, then i would just have to assign array[0] for the smallest number.
    Before attempting to sort an array start off simple ie create another variable to keep track of the smallest number. Take some time to come up with an algorithm for this program.

Popular pages Recent additions subscribe to a feed

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