Thread: Help with my code plz!

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    20

    Help with my code plz!

    Code:
     
    
    #include <stdio.h>
    struct numbers
    {
    	int number[1][4];
    }
    
    void calculate (int weeks)
    
    {
    	struct numbers week_number[weeks];
    	int i=0, x=0, a, b,y;
    
    
    	do
    	{
    		for (a=1;a<=2;++a)
    		{
    			printf("\nFor week %i, game %i, enter the two teams and the score: ",x+1, a);
    			scanf("%i%i%i%i", week_number[x].number[1][1], week_number[x].number[1][2], week_number[x].number[1][3], week_number[x].number[1][4]);
    			i=i+1;
    		}
    		x=x+1;
    
    	}while(x!=weeks);
    
    	for (b=0;b>=i;++b)
    	{
    		printf("\nWeek %i: %i-%i-%i-%i,", b, week_number[b].number[1][1], week_number[b].number[1][2], week_number[b].number[1][3], week_number[b].number[1][4]);
    	}
    }
    
    
    main()
    {
    
    	int weeks;
    
    	printf("How many weeks of data: ");
    	scanf("%i",&weeks);
    
    	calculate(weeks);
    }

    Its giving me two errors...

    1. To many types in declaration (Code Line: void calculate (int weeks))

    2. Constant expression required in function calc( Code Line: struct numbers week_number[weeks]

    ----------------------------
    Please help!

    Im new to programming.. I've been playing arround with structers and was woundering if i could use matrixes in them...

    Is this possible? Whats wrong with my code?

    Thanks!

    -valt

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct numbers
    {
    	int number[1][4];
    }
    You're missing the ; at the end of that last line.

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

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    20
    omg are you serious.. lol Thanks, ill try this when i get at school

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Please use more decsriptive titles in future. Something about what error you're getting or what you don't understand helps people see if the thread is about something they can help with.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    void calculate (int weeks)
    {
    	struct numbers week_number[weeks];
    Unless your compiler supports VLAs, you may need to think about doing some dynamic allocation using malloc.

    Code:
    int main(void)
    {
       ...
       return 0;
    }
    Should consider adding those parts in blue above.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    20
    Code:
    #include <stdio.h>
    
    
    struct numbers
    {
    	int number[1][4];
    	int win;
    	int loss;
    };
    
    
    
    void calc(int weeks)
    
    {
    
    	struct numbers week_number[100][2], team[4];
    	int i, week=0, a, game, win_point, loss_point;
    
    
    	do
    	{
    		game=1;
    		for (a=1;a<=2;++a)
    		{
    			printf("For week %i, game %i, enter the two teams and the score: ",week+1,a);
    			scanf("%i%i%i%i", &week_number[week][game].number[1][1], &week_number[week][game].number[1][2], &week_number[week][game].number[1][3], &week_number[week][game].number[1][4]);
    			game=game+1;
    		}
    
    		week=week+1;
    
    	}while(week!=weeks);
    
    
    
    	for(i=0;i>=4;++i)
    	{
    		team[i].win=0;
    		team[i].loss=0;
    
    		i=i+1;
    	}
    
    
    
    
    	week=0;
    	do
    	{
    		for(game=1;game<=2;++game)
    		{
    			if (week_number[week][game].number[1][3]>week_number[week][game].number[1][4])
    			{
    				win_point=week_number[week][game].number[1][1];
    				loss_point=week_number[week][game].number[1][2];
    				team[win_point].win=1+team[win_point].win;
    				team[loss_point].loss=1+team[loss_point].loss;
    			}
    			else
    			{
    				win_point=week_number[week][game].number[1][2];
    				loss_point=week_number[week][game].number[1][1];
    				team[win_point].win=(1+team[win_point].win);
    				team[loss_point].loss=(1+team[loss_point].loss);
    			}
    		}
    
    		week=week+1;
    
    	}while(week!=weeks);
    
    	printf("\nLeague Standings after %i weeks: ", weeks);
    
    
    	for(i=0;i>=4;++i)
    	{
    		printf("Team %i	%i-%i",i, team[i].win, team[i].loss);
    	}
    }
    
    
    
    int main(void)
    
    {
    	int weeks;
    
    	printf("How many weeks of data: ");
    	scanf("%i",&weeks);
    
    	calc(weeks);
    	return 0;
    }
    Thanks for all the feedback!

    um.. I still cant use weeks for my matrix, number[weeks][2], its not a constant variable or something... so i just made it 100 and it fixed it, but is there any other way so i can have weeks in there instead of 100... Because what if someone wants more then 100 weeks...

    Code:
    struct numbers week_number[100][2], team[4];


    Also, this program compiles, but doesnt display

    Code:
    	for(i=0;i>=4;++i)
    	{
    		printf("Team %i	%i-%i",i, team[i].win, team[i].loss);
    	}
    for some reason..

    please help, and thanks again!

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Think about this: for(i=0;i>=4;++i)

    You're telling it to keep looping as long as what's true? And is that true when the loop starts?
    If you understand what you're doing, you're not learning anything.

  8. #8
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    And as for your question about changing the size of your matrix, I suggest you do some research (I did all mine with the serach link at the top of this page) on malloc and realloc. Incredibly useful functions when used correctly.

  9. #9
    Registered User coolshyam's Avatar
    Join Date
    Mar 2005
    Posts
    26

    Lightbulb Malloc

    the array you provided takes up extra space in your RAM
    so, the concept of malloc is used. malloc dosent increase space but takes the extra space to the hard disk. it will execute the program in chunks. the active part of the program will be in the RAM

    here goes the syntax
    Code:
    char *p;
    p=(char *)malloc(300000);

    any questions any type in programming

    a ready made answer
    -----------------------------------------------------------------------------------
    FORTUNE FAVOURS THE BOLD!
    -----------------------------------------------------------------------------------

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by coolshyam
    the array you provided takes up extra space in your RAM
    so, the concept of malloc is used. malloc dosent increase space but takes the extra space to the hard disk. it will execute the program in chunks. the active part of the program will be in the RAM

    here goes the syntax
    Code:
    char *p;
    p=(char *)malloc(300000);
    Um no. There's nothing in the standard that says malloc must use your hard disk if it runs out of space. Why? Well because some things don't have "hard disks". What about embeded devices? What you're talking about is strictly an OS issue of how it handles running out of memory. malloc does no such thing.

    Oh, and don't typecast the return value of malloc.

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

  11. #11
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    To use malloc() in your example, simply do something along these lines:

    From:
    Code:
    void calculate (int weeks)
    
    {
    	struct numbers week_number[weeks];
    To:
    Code:
    void calculate (int weeks)
    {
            struct numbers* week_number = malloc(sizeof(struct numbers) * weeks);
    If you wanted to have a multi-dimensional array like you did before with [100][2], you can do something like the following:
    Code:
    for (i = 0; i < 2; i++)
    {
            struct numbers* week_numbers[i] = malloc(sizeof(struct numbers) * weeks);
    }

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Orion
    If you wanted to have a multi-dimensional array like you did before with [100][2], you can do something like the following:

    Code:
    for (i = 0; i < 2; i++)
    {
            struct numbers* week_numbers[i] = malloc(sizeof(struct numbers) * weeks);
    }
    Code:
    struct numbers *week_numbers[2];
    for (i = 0; i < 2; i++)
    {
            week_numbers[i] = malloc(sizeof(struct numbers) * weeks);
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by 0rion
    If you wanted to have a multi-dimensional array like you did before with [100][2], you can do something like the following:
    Code:
    for (i = 0; i < 2; i++)
    {
            struct numbers* week_numbers[i] = malloc(sizeof(struct numbers) * weeks);
    }
    No. Assuming it even compiles, guess what happens each time you run through your loop. You cause a memory leak. Why? Well, because you're declaring a new "struct numbers*" variable named 'week_numbers' each time the loop starts. Then, again, assuming it actually compiles, you stand a very good chance at crashing your program. Why? Because again, assuming it even compiles, you've just declared the following array:
    Code:
    struct numbers* week_numbers[i] = ...
    Where i is both the size of the array, and the index beyond the size of the array. Not good.

    Oh, and I forgot to mention something earlier for the OP. THIS:
    Code:
    struct numbers
    {
    	int number[1][4];
    };
    is the exact same thing as THIS:
    Code:
    struct numbers
    {
    	int number[4];
    };
    There is never* any reason to have an array of 1 element in size. You might as well just use a standard non-array variable.

    *There is I suppose a "just for kicks" reason of doing this, so that you can use the array name like a pointer instead of using the address-of operator, but there's no real practical purpose to it.

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

  14. #14
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Ahh *slaps himself in the face* musta been half asleep when I wrote that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong with my C code, PLZ help!!!
    By Eng_Girl in forum C Programming
    Replies: 5
    Last Post: 03-25-2006, 11:17 AM
  2. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Replies: 9
    Last Post: 10-18-2004, 09:14 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM