can someone help with an array

This is a discussion on can someone help with an array within the C Programming forums, part of the General Programming Boards category; I am trying to have a user enter 5 numbers and store them in an array and then have the ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    55

    can someone help with an array

    I am trying to have a user enter 5 numbers and store them in an array and then have the user enter an additional number and multiply the 5 numbers by it. But I can't get even get an array to accept 5 numbers. Can someone please help,

    code:
    #include <stdio.h>
    #include <stdlib.h>

    #define N 5

    int main(void)
    {
    int a[N];
    int i, sum=0, factor;

    printf("\nPlease enter 5 numbers\n");
    for (i=0;i<N; ++i);
    a[i]=0+i*i;
    printf("\nPlease enter a factor:\t");
    scanf("%d",&factor);





    return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    This should allow you to accept 5 numbers.
    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    
    #define N 5
    
    int main(void)
    {
    int a[N];
    int i, sum=0, factor;
    
    printf("\nPlease enter 5 numbers\n");
    for (i=0;i<N; ++i);
    {
      printf("Number %d: ", i+1);
      scanf("%d", &a[i])
    }
    
    printf("\nPlease enter a factor:\t");
    scanf("%d",&factor);
    
    return 0;
    }

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Look at my example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SIZE 5 /* size of array */
    
    int main(void)
    {
        int i, factor, arr[SIZE];
        
        /* get the numbers */
        for (i=0; i<SIZE; ++i)
          scanf("%d",&arr[i]);
        
        /* get the factor */
        printf("Insert the factor: ");
        scanf("%d",&factor);
        
        /* multiply all them by factor */
        for (i=0; i<SIZE; ++i)
          arr[i] *= factor;
          
        /* display array */
        for (i=0; i<SIZE; ++i)
           printf("%d ",arr[i]);
        
        /* pause */
        system("PAUSE");
        
        /* exit */
        return 0;
    }
    You have some problems in your code, like here:
    Code:
    for (i=0;i<N; ++i);
    In this case you dont need to ; the for command, you just need to close it when you want a null loop.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    Cool thanks that helps. I have never been good at arrays. C programming is kind of new to me. It makes more sense now.

    After multiplication are the new numbers automatically stored in the Array or do I need to put them in there somehow?

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by ct26torr
    After multiplication are the new numbers automatically stored in the Array or do I need to put them in there somehow?
    That's done here:
    arr[i] *= factor;

    arr[i] is set to the value arr[i] * factor.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    Cool..... I wrote this piece to sum the the array and it works but I only want to carry out 2 decimal places and I have like 6.

    Code:
    	/*sum the array*/
    	sum=0;
    	for (i=0; i<SIZE; ++i)
    		sum +=(SIZE+i);
    	printf("\nThis is the sum of the new Array values: %f\n", sum);

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Lookup the format modifiers for printf()

    >printf ("%.2f\n", myfloat);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    I added a line to try to calculate the average but it doesn't come put right. If I enter 1,2,3,4,5 in the array and a factor of 2 the array returns 2,4,6,8,10 like it should but when I divide by 5 it comes up with 7.

    Code:
    	/*sum the array and calculate average*/
    	sum=0;
    	average=0;
    	for (i=0; i<SIZE; ++i)
    		sum +=(SIZE+i);
    		average = (sum/5);
    	printf("\nThe average of the new Array values is: %f\n", average);

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>sum +=(SIZE+i);
    Think about this line. What exactly are you adding up here?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    I must be doing something wring in this code because it totals the values wrong.

    Code:
    	/*sum the array and calculate average*/
    	sum=0;
    	average=0;
    	for (i=0; i<SIZE; ++i)
    		sum +=(SIZE+i);
    		average = (sum/5);
    	printf("\nThe average of the new Array values is: %.2f\n", sum);

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I must be doing something wring in this code because it totals the values wrong.
    You read my previous post? How did you get on finding your problem?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    That's because in the line

    sum +=(SIZE+i);

    you're not adding anything from the array. You're adding SIZE (5) + 0, 5 + 1, 5 + 2, 5 + 3, 5 + 4, which comes out to 35. and 35 / 5 equals 7.
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  13. #13
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    I think I just fixed it. This is what I did.

    Code:
    	sum=0;
    	average=0;
    	for (i=0; i<SIZE; ++i)
    		sum += arr[i];
    		average = (sum/5);
    	printf("\nThe average of the new Array values is: %.2f\n", average);

  14. #14
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Yep. congrats
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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