Thread: Array question

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    Array question

    I am working on this problem



    1) Write array definitions for the following:

    a) A list of 5 integer houses

    b) A list of 20 double precision autoSales

    c) A list of 12 characters, each representing a grade



    I have come up with this so far... Is this close to being right at all? lol



    Code:
     
    
    #include <stdio.h>
    
    #include <conio.h>
    
    #define MAXHOUSES 5
    #define MAXAUTOSALES 20
    #define MAXGRADES 12
    
    int main()
    
    {
    	int houses [MAXHOUSES];
    	int h = 0 ;
    
    		/*Input Houses */
    
    		for (h = 0; h < MAXHOUSES; h++)
    		{
    			houses[h] = houses[h] + 5;
    		}
    
    	double autosales [MAXAUTOSALES];
    	int a = 0;
    
    	/*Input Autosales */
    
    		for (a = 0; a < 20; a++)
    		{
    			autosales[a] = autosales[a] + 1000.99;
    		}
    
    		
    	char grades [MAXGRADES];
    	int g = 0 ;
    
    		/*Input Houses */
    
    		for (g = 0; h < MAXGRADES; g++)
    		{
    			grades[g] = grades[g] + 5;
    		}
    
    	/*Print Report*/
    
    		printf("Houses: %d", houses[h]);
    		printf("\n\nAutosales: %.2f", autosales[a]);
    		printf("\n\nGrades: %c", grades[g]);
    		
    
    	
    
    
    } // end of main()

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    <conio.h> is a non-standard header file, and, thankfully, you're not using anything from it, so you could leave it out.

    Code:
    	/*Print Report*/
    
    		printf("Houses: &#37;d", houses[h]);
    		printf("\n\nAutosales: %.2f", autosales[a]);
    		printf("\n\nGrades: %c", grades[g]);
    By this point, h will be equal to MAXHOUSES, a will be MAXAUTOSALES, etc, so you'll be accessing one past the end of those arrays.

    But, looking at your problem:
    1) Write array definitions for the following:

    a) A list of 5 integer houses

    b) A list of 20 double precision autoSales

    c) A list of 12 characters, each representing a grade
    This should solve part a):
    Code:
    #define MAXHOUSES 5
    int houses [MAXHOUSES];
    etc.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    Hi

    Thanks for the speedy reply. I have been yelled at several times for using
    Code:
    #include <conio.h>
    on these forums however my teacher said we should include it... lol. Anyway I see what you wrote and as far as what you said for "this should solve a" didn't I already do that? I compiled the code and I'm getting some sort of crazy output. The only thing that actually prints is Houses: (with a series of 7 wierd numbers afterwards).

    What I wanted to do was print 5 values for houses (integers)

    So I made a for loop where I wanted to add five to the starting integer until the MAXHouses was hit. And so on for autosales, and grades... I don't know what going on! lol

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by GCNDoug View Post
    Thanks for the speedy reply. I have been yelled at several times for using
    Code:
    #include <conio.h>
    on these forums however my teacher said we should include it... lol.
    You need it if you're using a non-standard function like getch(), getche(), clrscr(), gotoxy(), etc.
    Anyway I see what you wrote and as far as what you said for "this should solve a" didn't I already do that? I compiled the code and I'm getting some sort of crazy output. The only thing that actually prints is Houses: (with a series of 7 wierd numbers afterwards).

    What I wanted to do was print 5 values for houses (integers)

    So I made a for loop where I wanted to add five to the starting integer until the MAXHouses was hit. And so on for autosales, and grades... I don't know what going on! lol
    Oh I see. I read the assignment as "declare a variable that will do this".

    Well, you just about got it, except that where you print the values, use
    Code:
    printf("Houses: %d", houses[MAXHOUSES-1]);
    etc.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for (g = 0; h < MAXGRADES; g++)
    Mmmm.

    > grades[g] = grades[g] + 5;
    What is the old value that 5 is added to?

    > however my teacher said we should include it
    Remind them of which millennium they're living in, which compiler you're using, on which OS.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Umm I wanted it to start at a certain letter and add five to that using the asci codes I guess I need to add 5 to the code and print it as a char? We are using Visual Studio 6.0 although I am using 2005; the classes uses xp pro, I use vista ultimate. My teacher is old and not the most helpful person in the world considering it's a hybrid class; but yeah she said we should include it. I don't know... lol


    Update

    Actually I just realized that usually we do need to include the getch() because at times the teacher just wants the executable. I think Visual Studio says to put a underscore in front. Is that correct?
    Last edited by GCNDoug; 04-11-2007 at 04:12 PM.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Actually I just realized that usually we do need to include the getch() because at times the teacher just wants the executable. I think Visual Studio says to put a underscore in front. Is that correct?
    Well, MSVC implements Borland's getch() as _getch(), but neither function is standard. See this FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    > grades[g] = grades[g] + 5;
    What is the old value that 5 is added to?
    Hint: don't you want to add 5 to the previous grade?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    yes, do I need to write a for loop for each one of my prince statements? The output is still way off. I made the changes you guys suggested.


    Code:
    #include <stdio.h>
    
    #include <conio.h>
    
    #define MAXHOUSES 5
    #define MAXAUTOSALES 20
    #define MAXGRADES 12
    
    int main()
    
    {
    	int houses [MAXHOUSES];
    	int h = 0 ;
    
    		/*Input Houses */
    
    		for (h = 0; h < MAXHOUSES; h++)
    		{
    			houses[h] = houses[h] + 5;
    		}
    
    	double autosales [MAXAUTOSALES];
    	int a = 0;
    
    	/*Input Autosales */
    
    		for (a = 0; a < 20; a++)
    		{
    			autosales[a] = autosales[a] + 1000.99;
    		}
    
    		
    	char grades [MAXGRADES];
    	int g = 0 ;
    
    		/*Input Houses */
    
    		for (g = 0; g < MAXGRADES; g++)
    		{
    			grades[g] = grades[g] + 5;
    		}
    
    	/*Print Report*/
    
    
    		
    
    		printf("Houses: %d", houses[MAXHOUSES - 1]);
    		printf("\n\nAutosales: %.2f", autosales[MAXAUTOSALES - 1]);
    		printf("\n\nGrades: %c", grades[MAXGRADES - 1]);
    		
    
    		_getch();
    		return 0;
    
    
    
    } // end of main()

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I made the changes you guys suggested.
    See the FAQ I mentioned. _getch() . . .

    Code:
    houses[h] = houses[h] + 5;
    See my other post.

    Also, you might want to return 0 at the end of main(). It's not required, but I think it makes it more readable.

    Also, declaring variables in the middle of a block is C99. You should declare g and a right after h if you want to retain C89 compatibility.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    I just realized I'm doing way more than I needed to do for this program lol. Just witting the declaration like you did was all I have to do. Opps!

  11. #11
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    It might be worth nothing that you have undefined behaviour in your code:

    Code:
    int houses[MAXHOUSES];
    houses[0] = houses[0] + 5;
    Salem pointed you already to that particular problem, but you probably couldn't figure out what he ment. The problem relies on the fact that the first line declares an array of 5 integers, but doesn't put any particular value into the 5 spots. The next line increments the value of houses[0], although there isn't a "defined" value in houses[0] yet.

    In order for your program to work correctly, you best initialize your array with all zero's at the start, like this:

    Code:
    int houses[MAXHOUSES] = {0};
    houses[0] = houses[0] + 5;
    Also, don't mix declarations and statements in your code and you are right that you need a loop to print all the values of an array.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In order for your program to work correctly, you best initialize your array with all zero's at the start, like this:

    Code:
    int houses[MAXHOUSES] = {0};
    houses[0] = houses[0] + 5;
    Well, actually, I think the OP would need something like
    Code:
    houses[x] = houses[x-1] + 5;
    (starting x at 1, of course).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Aha ! Your avatar IS able to read the board users mind ...

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by KONI View Post
    Aha ! Your avatar IS able to read the board users mind ...
    Sometimes; other times it just acts like a whirlpool, sucking in all of my ideas.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM