Arrays with no pointers - problem (please help!) 
Author Message
 Arrays with no pointers - problem (please help!)

I am stuck with the following exercise and request help.

*WITHOUT* using pointers - The program is supposed to set an array for 52
weeks (initialized to 0).  The user is then asked to enter hours for each
week.  As you can see from the attached code, I am having a tough time
setting up the structure of the array.  When the user quits, the program is
supposed to print the entire array with all the changes made by the hours
input.

Can anyone take a look at this and get it to execute?

If there is a "nospam" inside my return address, please remove it prior to
sending me e-mail.

Thanks!

Guy McDonald

-------------------
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MaxFirstName 20
#define MaxLastName 20
#define NumWeek 52
#define NumHours 80

struct Employee
{
int EmployeeNumber;
char EmployeeFirstName[MaxFirstName];
char EmployeeLastName[MaxLastName];
float EmpHours [NumWeek][NumHours];

Quote:
};

void GetString(char [], int);
struct Employee InitializeEmployee(struct Employee);
void PrintEmployee(struct Employee);
struct Employee GetWeek(struct Employee);
struct Employee GetHours(struct Employee);

int main(void)
{
struct Employee OneEmployee;
OneEmployee=InitializeEmployee(OneEmployee);
OneEmployee=GetWeek(OneEmployee);
OneEmployee=GetHours(OneEmployee);

/*NumHours=InEmployee(EmpHours[NumWeek-1]); */
/*InEmployee.EmpHours[Incr]=0; */

for (Incr=0;Incr<NumWeek;Incr++)

do
{
  printf("Enter the week number between 1 and 52 or q to exit: ");
  scanf("%d",&NumWeek);
  printf("Enter the number of hours you worked that week or q to exit: ");
  scanf("%d",&NumHours);

Quote:
}

while (NumWeek !='q');

PrintEmployee(OneEmployee);
return 0;

Quote:
}

void GetString(char InString[], int Length)
{
char Input;
int Incr;
Input = getchar();
for (Incr=0;(Incr<(Length-1))&&(Input!='\n');Incr++)
{
  InString[Incr] = Input;
  Input = getchar();
Quote:
}

InString[Incr] = '\0';
fflush(stdin);

Quote:
}

struct Employee InitializeEmployee(struct Employee InEmployee)

{
int Incr;
printf("\nEnter the employee ID number: ");
fflush(stdin);
scanf("%d", &InEmployee.EmployeeNumber);
fflush(stdin);
printf("\nEnter the employee first name: ");
GetString(InEmployee.EmployeeFirstName, MaxFirstName);
printf("\nEnter the employee last name: ");
GetString(InEmployee.EmployeeLastName, MaxLastName);

for(Incr=0;Incr<NumWeek;Incr++)
return InEmployee;

Quote:
}

void PrintEmployee(struct Employee InEmployee)
{
int Incr;
int LineCount=0;
printf("\n\nEmployee number: %d", InEmployee.EmployeeNumber);
printf("\nEmployee name  : %s %s", InEmployee.EmployeeFirstName,
InEmployee.EmployeeLastName);

for(Incr=0;Incr<NumWeek;Incr++)

{
  LineCount = LineCount + 1;
  if (LineCount == 11);
  {
   printf("\n");
   LineCount = 1;
  }
  printf("\%4.1f  ", InEmployee.EmpHours[Incr]);

Quote:
}
}



Sat, 03 Feb 2001 03:00:00 GMT  
 Arrays with no pointers - problem (please help!)

Quote:

>I am stuck with the following exercise and request help.

>*WITHOUT* using pointers - The program is supposed to set an array for 52
>weeks (initialized to 0).  The user is then asked to enter hours for each
>week.  As you can see from the attached code, I am having a tough time
>setting up the structure of the array.  When the user quits, the program is
>supposed to print the entire array with all the changes made by the hours
>input.

>Can anyone take a look at this and get it to execute?

>If there is a "nospam" inside my return address, please remove it prior to
>sending me e-mail.

>Thanks!

>Guy McDonald


Do your own homework!


Sun, 04 Feb 2001 03:00:00 GMT  
 Arrays with no pointers - problem (please help!)

Quote:

> *WITHOUT* using pointers - The program is supposed to set an array for 52
> weeks (initialized to 0).  The user is then asked to enter hours for each
> week.  As you can see from the attached code, I am having a tough time
> setting up the structure of the array.  When the user quits, the program is
> supposed to print the entire array with all the changes made by the hours
> input.
> #include <stdio.h>
> #include <conio.h>

This is not a standard C header file. And it is of no use to a simple
program like yours.

Quote:
> #include <string.h>
> #define MaxFirstName 20
> #define MaxLastName 20
> #define NumWeek 52
> #define NumHours 80

Hmmm, this "NumHours" defined as 80 does not occur in your assignment
description. What is it supposed to mean ?

Quote:
> struct Employee
> {
> int EmployeeNumber;
> char EmployeeFirstName[MaxFirstName];
> char EmployeeLastName[MaxLastName];
> float EmpHours [NumWeek][NumHours];

The "EmpHours" is a two dismensional array. AFAICS  your description
requires only a one dimensional array here. You want to store *one*
number per week. Your 2D array allows entering 80 numbers per week.
I'd say You should change this to:
  float EmpHours [NumWeek];

Quote:
> int main(void)
> {
> struct Employee OneEmployee;
> OneEmployee=InitializeEmployee(OneEmployee);

I understand that you have to return "OneEmployee" due to not being
allowed to use pointers. But please understand that using pointers
will make this a bit easier.

Quote:
> OneEmployee=GetWeek(OneEmployee);
> OneEmployee=GetHours(OneEmployee);

These two functions are not included in the code that you have posted.

Quote:
> /*NumHours=InEmployee(EmpHours[NumWeek-1]); */
> /*InEmployee.EmpHours[Incr]=0; */

???

Quote:
> for (Incr=0;Incr<NumWeek;Incr++)

Is this really supposed to be a "for" loop that contains a while loop ?
Instead it looks like source code that good severely damaged by
pasting it into the posting. Please check this.

Quote:
> do
> {
>   printf("Enter the week number between 1 and 52 or q to exit: ");
>   scanf("%d",&NumWeek);

"NumWeek" is a "#define" macro, it is *not* a variable. It can
not be modified.

Quote:
>   printf("Enter the number of hours you worked that week or q to exit: ");
>   scanf("%d",&NumHours);

"NumHours" is a "#define" macro, it is *not* a variable. It can
not be modified.

Quote:
> }
> while (NumWeek !='q');

The end of loop condition is dangerously wrong. For one thing, because
"NumWeek" is a macro. But even if "NumWeek" would be a variable, it
would be a number (hopefully a week number) and comparing a number
to a letter will not give you what you want.

And this loop essentially does nothing more than input lots of numbers
that are not used at all. I'd have hoped to see something like:
  OneEmployee.EmpHours[weekNumber] = numberOfHours;

Please be aware that "scanf()" is a very dangerous and confusing
function. It looks deceptively easy to use, but is full of deadly
traps and pitfalls and gotchas. Especially when used inside a loop.

"fgets()" look more complicated to use, but will be a lot more
reliable and predictable.

Quote:
> void GetString(char InString[], int Length)

Hah, you are using pointer, without noticing it :-)

BTW, the standard C function "fgets()" does almost exactly what you
do in this function: It reads a line of text from the user. The main
difference is that it includes the "\n" in the string and that you
must specify "stdin" specifically:

  /* Read one line and remove trailing '\n' (without using pointers) */
  fgets( inString, maxSize, stdin );
  length = strlen(inString];
  if ( inString[length] == '\n' )  inString[length] = \0';

Quote:
> fflush(stdin);

This is generally *not* a good idea. I've seen that you use this a lot.
But the C standard says that "fflush()" is *not* defined for input
streams. With most compilers it will simply do nothing, with some it
might crash the program.

Using "fgets()" will typicallly work without having to empty the
input buffer.

Quote:
> struct Employee InitializeEmployee(struct Employee InEmployee)

[snip]

Quote:
> for(Incr=0;Incr<NumWeek;Incr++)

Here is another of those empty loops that does not have a loop body.
What is it supposed to do ?

Quote:
> return InEmployee;
> }

[rest of code snipped]

I'd suggest that you have a good look at my comments and do some
serious rewriting of your code. If you can't get it to work, simply
ask again. But please try to be specific about what exactly your
problem is. For instance the above code does not compile at all
and is full of strnage errors. Was this your main problem ? Try
to pay close attention to compiler error messages. If you have some
code and do not understand the error message, you might ask here
about that message specifically.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sun, 04 Feb 2001 03:00:00 GMT  
 Arrays with no pointers - problem (please help!)

Quote:

>> >Can anyone take a look at this and get it to execute?

>> Do your own homework!

>I must agree with him in saying 'do your own work' it's the only way you
>will learn.  If you have a specific question about C then ask, but don't
>ask for someone to write you a program, so you can get good marks.

HE DIDN'T.  There are plenty of people who do just state their assignment
and expect people to complete it for them, but this was one of the few who
had made a decent stab at the problem and showed his work so far.  Such
people should be supported, not vilified.

Helping people help themselves is one of the things Usenet is *for*.
Stephan has already posted a long reply, which I see concludes with a
suggestion to try to pin down the problem more specifically and some hints
on how to do it.  Good advice -- but not necessarily obvious advice -- and
not something which makes the original question invalid or improper.

Cheers,
Richard
--
Richard Stamp, Cambridge, UK



Sun, 04 Feb 2001 03:00:00 GMT  
 Arrays with no pointers - problem (please help!)
So far I can say thank you to several people - Stephen Wilms, Charles
LaCour, William Kaufman.

As for James Atkinson & some guy who won't even post his name using a
DejaNews account - here's a little sage advice from one who has been around
the block a few times.

Please don't expect your contemporaries to help you after posting messages
like "do your own homework."  As a matter of fact, I have done my homework.
As a 35 year old technical writer, I thought it wise to try and better
myself by learning some computer languages.  C and Visual Basic are good
choices considering today's market.  It is very frustrating for a tech
writer to talk with an SME and not understand syntax definitions or
rudimentary functions that they are talking about.  To date, I am halfway
through my second class on C and never posted a plea for help until now.  So
when I come to a newsgroup and post a note asking for help in a class - it's
because I have done everything possible with the instructor & textbook to
try and understand the exercise.  It is not because I am some lazy cheat
looking for a free ride.  Shame on you Mr Atkinson & Mr. Unknown.  You have
more to learn than you may ever come to realize.

Sincerely,

Guy McDonald

Quote:



>>> >Can anyone take a look at this and get it to execute?

>>> Do your own homework!

>>I must agree with him in saying 'do your own work' it's the only way you
>>will learn.  If you have a specific question about C then ask, but don't
>>ask for someone to write you a program, so you can get good marks.

>HE DIDN'T.  There are plenty of people who do just state their assignment
>and expect people to complete it for them, but this was one of the few who
>had made a decent stab at the problem and showed his work so far.  Such
>people should be supported, not vilified.

>Helping people help themselves is one of the things Usenet is *for*.
>Stephan has already posted a long reply, which I see concludes with a
>suggestion to try to pin down the problem more specifically and some hints
>on how to do it.  Good advice -- but not necessarily obvious advice -- and
>not something which makes the original question invalid or improper.

>Cheers,
>Richard
>--
>Richard Stamp, Cambridge, UK



Sun, 04 Feb 2001 03:00:00 GMT  
 Arrays with no pointers - problem (please help!)

Quote:
>> >Can anyone take a look at this and get it to execute?

>> Do your own homework!

>I must agree with him in saying 'do your own work' it's the only way you will
>learn.  If you have a specific question about C then ask, but don't ask for
>someone to write you a program, so you can get good marks.

Excuse me. What sort of attitude is this??? He has obviously put some
effort into his assignment. And he hasn't asked anyone to write a
program for him, only to help him out with it. He doesn't deserve this
sort of blatantly unfair criticism. It's not right that this sort of
response can go unnoticed in a newsgroup like this one.

--
"I see you have books under your arm, brother. It is indeed a rare pleasure
these days to come across somebody that still reads, brother."

        - Anthony Burgess



Sun, 04 Feb 2001 03:00:00 GMT  
 Arrays with no pointers - problem (please help!)

Quote:

> It is not because I am some lazy cheat
> looking for a free ride.  Shame on you Mr Atkinson & Mr. Unknown.  You have
> more to learn than you may ever come to realize.

Hmmm, don't be too harsh in your judgement. It is often difficult
to distinguish lazy pupils from serious and honest requests. And
we get lot's of lazy pupils (or students) here in c.l.c !

The presence of some code is generally a good sign of a bona fide
attempt on the side of the poster. You posted your code and I liked
and respected that. I think the "homework" acusation was very ill
advised and stems from a lack of proper attention on the side
of the acusers.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Mon, 05 Feb 2001 03:00:00 GMT  
 Arrays with no pointers - problem (please help!)

Quote:


>> It is not because I am some lazy cheat
>> looking for a free ride.  Shame on you Mr Atkinson & Mr. Unknown.  You
have
>> more to learn than you may ever come to realize.

>Hmmm, don't be too harsh in your judgement. It is often difficult
>to distinguish lazy pupils from serious and honest requests. And
>we get lot's of lazy pupils (or students) here in c.l.c !

>The presence of some code is generally a good sign of a bona fide
>attempt on the side of the poster. You posted your code and I liked
>and respected that. I think the "homework" acusation was very ill
>advised and stems from a lack of proper attention on the side
>of the acusers.

Just to clear things up... I posted the 'Do your own homework' responce
becuase I had just finished reading a prior post by the same person in
comp.programming that 1) didnt' show ANY code and 2) was just a word for
word copy of his assignment. With this evidence I figured he was a 'lazy'
student looking for someone to do his assignment. I feel no shame for my
opinions, if you can't take a little flame get off the internet :)

James Atkinson



Mon, 05 Feb 2001 03:00:00 GMT  
 Arrays with no pointers - problem (please help!)

Quote:

> Just to clear things up... I posted the 'Do your own homework' responce
> becuase I had just finished reading a prior post by the same person in
> comp.programming that 1) didnt' show ANY code and 2) was just a word for
> word copy of his assignment. With this evidence I figured he was a 'lazy'
> student looking for someone to do his assignment. I feel no shame for my
> opinions, if you can't take a little flame get off the internet :)

TIP:  If a post by someone offends you, tell them so in response to that
post, not some other post.  If this is not possible, at least try to flame
them on the same newsgroup.  When you flame an innocent post and represent
it as something else, you come off looking like an {*filter*}, but if you
flame the actual guilty post, you don't. (At least not as much)

--

(supporter of the campaign against grumpiness in c.l.c)
"The most cost effective way to make your business
explode!" --An unknown spammer



Fri, 09 Feb 2001 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Please help~~Array and Pointer

2. Pointer to a struct within an array Help Please

3. NEED HELP WITH PRITING AN ARRAY, PLEASE PLEASE HELP

4. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

5. problems with array allocation and destruction::: HELP PLEASE

6. Problem with array's PLEASE HELP

7. Problems with sorting arrays-Please Help

8. Problem allocating memory to pointer, please help

9. Problems with array elements from user input, please help

10. Please help solve a problem that deal with pointer to structure

11. array problem PLEASE HELP

12. Urgent please help LPSTR Array function passing problem

 

 
Powered by phpBB® Forum Software