1. ## 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

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;
}

int g = 0 ;

/*Input Houses */

for (g = 0; h < MAXGRADES; g++)
{
}

/*Print Report*/

printf("Houses: %d", houses[h]);
printf("\n\nAutosales: %.2f", autosales[a]);

} // end of main()

2. <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]);
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.

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.

3. ## 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. Originally Posted by GCNDoug
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.

5. > for (g = 0; h < MAXGRADES; g++)
Mmmm.

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.

6. 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?

7. 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

What is the old value that 5 is added to?

8. 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

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;
}

int g = 0 ;

/*Input Houses */

for (g = 0; g < MAXGRADES; g++)
{
}

/*Print Report*/

printf("Houses: %d", houses[MAXHOUSES - 1]);
printf("\n\nAutosales: %.2f", autosales[MAXAUTOSALES - 1]);

_getch();
return 0;

} // end of main()

9. 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.

10. 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. 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. 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).

13. Aha ! Your avatar IS able to read the board users mind ...

14. Originally Posted by KONI
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.