unknown size array

This is a discussion on unknown size array within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> main(void) { int days = 0,count; float x[ unknown size ], average, sum; printf("how many days?"); scanf("%d", &days); ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    77

    unknown size array

    Code:
    #include<stdio.h>
    
    main(void)
    {
    	int days = 0,count;
    	float x[unknown size],  average, sum;
    	printf("how many days?");
    	scanf("%d", &days);
    
    		for ( count = 0; count < days; count++) {
    			
    			sum = 0;
    			    printf("\n Day %d \n how many amount?", count + 1);
    				scanf("%f", &x[count]);
    				sum += x[count];
    		}
    
    	average = sum/days;
    	printf("\n the average sales is %f", average);
    	return 0;
    }
    how do i read a unknown size array?i had read i few example from this forum but still dont understand

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is no such thing in C. You either have to "oversize" your array (e.g. make it 1000 elements when you never expect more than a couple of hundred), or dynamically allocate the memory once you know the size needed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by matsp View Post
    There is no such thing in C. You either have to "oversize" your array (e.g. make it 1000 elements when you never expect more than a couple of hundred), or dynamically allocate the memory once you know the size needed.

    --
    Mats
    but then this will come out in exam said by my teacher..


    *formula = average = day 1 sales+......+ day n sales / n

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To calculate the average, you do not need to store the values in an array - just read in a value at a time and add it to the total - don't need it after you've added it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by matsp View Post
    To calculate the average, you do not need to store the values in an array - just read in a value at a time and add it to the total - don't need it after you've added it.

    --
    Mats
    so where i modified the code?i dont really understand by sentence.. can u point where should i modified?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You needn't make "x" an array - a single float x will be sufficient. And of course, you do not need to index with count.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by matsp View Post
    You needn't make "x" an array - a single float x will be sufficient. And of course, you do not need to index with count.

    --
    Mats
    oh but if like that then my code don't have array.. using array is one of the requirement of the question..

    Q:* the accountant is able to type in the entire daily sales ammount into the program

    * use forumula average = day 1+....+ day n sales/n

    *use loop,fuction pointer and array for your programme

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    Quote Originally Posted by archriku
    oh but if like that then my code don't have array.. using array is one of the requirement of the question..
    Right. So what have you learnt about malloc(), realloc() and free()?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by matsp View Post
    There is no such thing in C.
    C99 disagrees with you. Declare the array after you read the size.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    Quote Originally Posted by quzah
    C99 disagrees with you.
    However, this is not likely to be C99 since default int was used.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I fail completely to see what function pointers have to do with this...

    Or is it supposed to say "function, pointers"?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Did you stumble into the wrong thread? You're the only one to say the word "funtion" prior to my usage.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by laserlight View Post
    However, this is not likely to be C99 since default int was used.
    I'm not a fan of them anyway. I was just saying it is an option in Standard C now.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by quzah View Post
    C99 disagrees with you. Declare the array after you read the size.


    Quzah.
    That means we can do something like this according to C99.
    Code:
    int main(void)
    {
    int n;
    printf("Enter the size");
    scanf("%d",&n);
    int arr[n];//is this declaration correct after scanf in C99
    }
    But again i doubt whether C99 allows any declaration at any place not just after the curly brace of main.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by BEN10 View Post
    But again i doubt whether C99 allows any declaration at any place not just after the curly brace of main.
    Actually it does.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocating an array of unknown size?
    By scarlet00014 in forum C Programming
    Replies: 2
    Last Post: 09-27-2008, 09:53 AM
  2. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  3. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Maximum array size?
    By code2big in forum C Programming
    Replies: 2
    Last Post: 05-25-2004, 08:16 AM

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