Thread: Array Program

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    2

    Array Program

    I am hoping some one can help me out. I am trying to write a program that can read in 10 float numbers and print out their values to the screen. I also need to determine the largest, smallest, sum, average. I need to use an array. I have typed up alot of the code already, but I am having problems trying to compile what I have coded so far. Below is my code. Any input would be appreciated.

    Code:
    #include <stdio.h>  // for library function: printf
    
    // function main begins program execution
    main()
    {
        int i;  // counter
        float n[10]; // n is an array of 10 integers
        float sum = 0; // sum of the array
        float average, smallest, largest;        
        
        printf( "Enter 10 float numbers:\n" );
        scanf( "%f", &n[10] );
             
        // initialise elements of array to 0
        for ( i = 0; i < 10; i++ )
        {
            n[i] = 0;  // set element at location i to 0
            }  // end for
            
         
        // sum of contents of array n
        for ( i = 0; i < 10; i++ )
        {
            sum += n[i];
            }  // end for
    	
        // average of contents of array n
        for ( i = 0; i < 10; i++ )
        {
            sum / n[i];
            }  // end for
            
        // arrange array in ascending order
        for ( i = 0; i < n - 1; i++ )
        {
            for j = 0; j < n - 1 - i; j++ )
            {
                inOrder( &num[j], &num[j+1] );
                
            }  // end for    
            
        // smallest number in array n
        **I have not done this as yet**
        
        // largest numnber in array n
        **I have not done this as yet**
       
    	printf( "The sum of array n is %f\n", sum );
    	printf( "The avarage of array n is %f\n", average );
    	printf( "The array in ascending order is %f\n", n[10] );
    	printf( "The smallest number of array n is %f\n", smallest );
    	printf( "The largest number of array n is %f\n", largest );
    	
    	
    	return 0; // indicates successful termination
    }  // end function main

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>// n is an array of 10 integers
    No, n is an array of floats

    >>scanf( "%f", &n[10] );
    Learn how to get numbers from the user

    >>// initialise elements of array to 0
    You read into the array from the user, then 0 out everything?? ::

    >>sum / n[i];
    That won't compute the average and store it. The average is total/count, or in your case sum/10.

    >>for ( i = 0; i < n - 1; i++ )
    "n - 1" isn't correct. Think about how many times you want to the loop to run, and where that number is going to come from

    >>for j = 0; j < n - 1 - i; j++ )
    Your compiler will tell you what's wrong with this one (there are three things).

    Write and compile your program in small steps to ensure each part works.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    2
    Thanks for your advice
    I am still having trouble - I read the article that you provided the link for and I have been working on this for quite some time now (about 6 hours since I got the reply - but in that time I have done some research and more reading). I have altered my code but I still have a problem - I cannot figure out what is wrong with the line:

    Code:
     
    // arrange array in ascending order
    for ( int i = 0; i < n; i++ )
    I get the message for this line: ISO C++ forbids comparison between (this is in Dev C++)


    here is my full code I have so far now:
    Code:
    #include <stdio.h>  // for library function: printf
    
    // function main begins program execution
    main()
    {
        int i;  // counter
        int swap = 0;
        float n[10]; // n is an array of floats
        float sum = 0; // sum of the array
        float temp, average, smallest, largest;        
         
        // initialise elements of array to 0
        for ( i = 0; i < 10; i++ )
        {
            n[i] = 0;  // set element at location i to 0
            }  // end for
            
        printf( "Enter 10 float numbers:\n" );
        scanf( "%f", &n[10] );
        
        // sum of contents of array n
        for ( i = 0; i < 10; i++ )
        {
            sum += n[i];
            }  // end for
    	
        // average of contents of array n
        for ( i = 0; i < 10; i++ )
        {
            sum / 10;
            }  // end for
            
        // arrange array in ascending order
        for ( int i = 0; i < n; i++; )  //  second attempt
        // 1st try - for ( int i = 0; i < (n - 1); i++ )
        {
            if ( n[i] > n[i + 1] )
            {
                temp = n[i];
                n[i] = n[i + 1];
                n[i + 1] = temp;
                swap = 1;
            }  // end if
            while (swap !=0);
            
                        
            }  // end for    
            
        // smallest number in array n
        //**I have not done this as yet**
        
        // largest numnber in array n
        //**I have not done this as yet**
       
    	printf( "The sum of array n is %f\n", sum );
    	printf( "The avarage of array n is %f\n", average );
    	printf( "The array in ascending order is %f\n", n[10] );
    	//printf( "The smallest number of array n is %f\n", smallest );
    	//printf( "The largest number of array n is %f\n", largest );
    	
    	
    	return 0; // indicates successful termination
    }  // end function main
    Any input would be appreciated
    Thankyou
    Last edited by emmx; 08-29-2003 at 11:37 AM.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Originally posted by emmx
    I cannot figure out what is wrong with the line:

    Code:
     // arrange array in ascending order
    for ( int i = 0; i < n; i++ )
    Look at your own comment.
    Code:
    int i;  // counter
    float n[10]; // n is an array of floats
    Your complier rightly complains about trying to compare an int value with an array of floats.

    There's plenty more going wrong here, though.
    Code:
       printf( "Enter 10 float numbers:\n" );
       scanf( "%f", &n[10] );
    This doesn't automagically read an array for you. Use a loop.
    Code:
        // average of contents of array n
        for ( i = 0; i < 10; i++ )
        {
            sum / 10;
            }  // end for
    Ten times you calculate sum / 10 and throw the result away. But at least you got the loop condition right. (But you don't need a loop to calculate an average if you have a sum and the number of values.)
    Code:
        for ( int i = 0; i < n; i++; )  //  second attempt
        // 1st try - for ( int i = 0; i < (n - 1); i++ )
        {
            if ( n[i] > n[i + 1] )
    Syntax error.
    Either C++ or C99.
    But more serious is the out-of-bounds array access, which is undefined behavior. (The first attempt was closer.)
    Code:
            while (swap !=0);
    So if swap is non-zero, just loop here forever? Google for sorting algorithms (perhaps specifically bubble sort).
    Code:
       printf( "The array in ascending order is %f\n", n[10] );
    This doesn't automagically print an array for you. Use a loop.



    Other ideas:
    • Re(re)ad previous replies.
    • Crank up the compiler's error/warning output.
    • Try to understand any diagnostic messages from the compiler.
    • Do your best to correct the program per diagnostic message.
    • Search for answers.
    • Re-post your code with any errors/warnings/behavior you don't understand.
    • Lather, rinse, repeat if necessary.
    Last edited by Dave_Sinkula; 08-31-2003 at 12:47 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Array Program
    By alex1067 in forum C Programming
    Replies: 5
    Last Post: 04-15-2008, 06:26 AM
  3. help with small program. array issue
    By InvariantLoop in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 12:26 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM