Thread: 1D array

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    9

    1D array

    Hi,

    I need your help and advise. Something is very wrong with my code, but I am not able to figure out all day long, I am totaly confused between my 1D arrays and functions, what needs to be "return ( )". Thanks!


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
         long id_number[5]={5466,5546,4432,3345,2345};
         float hours[5];
         float overtime[5];
         float gross[5];
    	 float deduction[5];
    	 float net_pay[5];
         float wage[5] = {10.60, 9.75, 10.50,12.25, 8.35};
    
    float get_hours (float wage[])
    {
         int temp, i = temp;
    
         for (temp=1; temp<6; ++temp)
         {
             printf ("Enter employee #%d hours worked: ",id_number[temp-1]);
             scanf ("%f", &hours[temp-1]);
        
         }
    
         return 0;
    }
    
    float calculate_overtime (float wage[])
    {
         int temp, i = temp ;
    	 float overtime;
         for (temp=1; temp<6; ++temp)
         {
             if (hours[temp-1] > 40)
                 overtime = hours[temp-1]- 40;
             else
                 overtime = 0;
         }
         return (overtime);
    }
    
    float calculate_gross (float wage[])
    {
         int temp, i = temp;
    
         for (temp=1; temp<6; ++temp)
         {
             if  (overtime[temp-1] > 0)
                 gross[temp-1] = (wage[temp-1]* 40) +
                 ((wage[temp-1] * 1.5) * overtime[temp-1]);
             else
                 gross[temp-1] = (wage[temp-1] * hours[temp-1]) +
                 ((wage[temp-1] * 1.5) * overtime[temp-1]);
         }
         return (gross[temp -1]);
    }
    
    float calculate_deduction (float gross)
    
    {
    	{	
    		int temp, i = temp;
    	
         for (temp=1; temp<6; ++temp)
    		 deduction[temp-1]=gross[temp-1]/3;
    }
    	
    
            return (deduction);
    }
    
    
    float calculate_net (float deduction)
    {
         int temp, i = temp;
    
         for (temp=1; temp<6; ++temp)
    		 net_pay[temp-1]=gross[temp-1]- deduction[temp-1];
    }
    
    return (net_pay[temp-1]);
    } 
    	
    void output (void)
    {
         int temp;
    
         printf ("\n\n");
         printf ("-----------------------------------------------------------------------\n");
         printf ("EM#    Pay Rate   Hours      OT       Gross Pay   Deduction   Net Pay\n");
         printf ("-----------------------------------------------------------------------\n");
    
         for (temp=1; temp<6; ++temp)
         {
              printf ("%d     $%5.2f      %5.1f     %5.1f     $%6.2f\n",
              id_number[temp-1], wage[temp-1], hours[temp-1],
              overtime[temp-1], gross[temp-1]),deduction[temp-1], net_pay [temp-1];
         }
    }
    
    int main ()
    {
         /*Execute functions*/
         get_hours (hours);
         calculate_overtime (overtime);
         calculate_gross (gross);
    	 calculate_deduction (deduction);
    	 calculate_net (net_pay);
         output ();
    
         printf ("\n");
         /*Keep window open*/
         system("PAUSE");
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Hmmm...yes you do have some real problems there.

    Your function 'get_hours' - you show it to return a float, and yet you have a 'return 0;' in the function. You should make it a void function. Also, in the same function, you have a printf() format specifier as %d - and yet your argument is of a long int type, therefore you should use a format specifier %ld

    Ack - this is a lot of stuff to be fixing. Perhaps you should go over your data types in your functions and make sure they all match. Also, you should state more specifically what your problem is - what sorts of things are going wrong, and so on - does your compiler give you any warnings? Does the program even compile, and can you run it?

    /~

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    Thanks so much,I know my question isn't really clear, and I do have a lot of problems. It did compile one time,
    I did get the table header print out, empl.# and hours for each employee , but I didn;t get any other results ( all "0" for my gross, net pay and OT column) in my output and there are few warnings like:

    initializing' : truncation from 'const double ' to 'float '
    let me go over again my code and tutorial thanks, but I am really confused.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Let's start at the top...
    Code:
    include <stdio.h>
    #include <stdlib.h>
    
         long id_number[5]={5466,5546,4432,3345,2345};
         float hours[5];
         float overtime[5];
         float gross[5];
    	 float deduction[5];
    	 float net_pay[5];
         float wage[5] = {10.60, 9.75, 10.50,12.25, 8.35};
    Eesh. Globals? Why. Ok, no real problem there, but you're usually better off avoiding globals as much as possible. If you insist on using them, make sure and initialize them all at creation time. Just like you have 'id_number' initialized, do the same for the rest. If you just want them all initialized to zero, do:
    Code:
    float hours[5] = { 0.0 };
    Next...
    Code:
    float get_hours (float wage[])
    {
         int temp, i = temp;
    
         for (temp=1; temp<6; ++temp)
         {
             printf ("Enter employee #%d hours worked: ",id_number[temp-1]);
             scanf ("%f", &hours[temp-1]);
        
         }
    
         return 0;
    }
    For starters, i isn't even used in this function, so get rid of it. If you do want it there for some reason, and you actually plan on using it, you should initialize 'temp' first.

    Otherwise what happens is that 'temp' has some random uninitialized value (re: it could be anything), and you're giving 'i' whatever that random value happens to be. I doubt you want that.

    As previously stated, you may as well make this function 'void', since you don't do anything with the return value.

    If you do want it to return something, since you're using a floating point number, you'll need to do:
    Code:
    return 0.0;
    And finally, your array indexes start at zero and go through size-1. So if your array is declared as 5, you have valid use of 0 through 4. It's clearer if you start your loop indexes accordingly, as this is much easier to read:
    Code:
    for (temp=0; temp<5; ++temp)
         {
             printf ("Enter employee #%d hours worked: ",id_number[temp]);
             scanf ("%f", &hours[temp]);
    No need to keep adding -1 to everything this way. Personal preference, but it's just clearer. Less cluttered.

    Next...
    Code:
    float calculate_overtime (float wage[])
    {
        ...same issue as above with 'i' and 'temp'...
    
        ....same issue with loop counter/array...
    
                 overtime = hours[temp-1]- 40;
             else
                 overtime = 0;
         }
         return (overtime);
    }
    Whenever you assign a value to a floating point number, you need to treat it as such and stop passing it integers.
    Code:
    overtime = 0.0;
    That would be the correct method. Or you could typecast 0 to float, but that wouldn't be the preferred method.

    Next...
    Code:
    float calculate_gross (float wage[])
    {
        ...same issue with 'i' and 'temp'...
    
        ...same issue with loop/array...
    }
    Nothing really wrong here, other than what I've already mentioned.

    Next...
    Code:
    float calculate_deduction (float gross)
    {
    	{
    		...temp and i issue again...
    
    		...loop/array issue again...
    			deduction[temp-1]=gross[temp-1]/3;
    }
            return (deduction);
    }
    Why do you have two sets of braces here? There is no need for them.

    Here you also see why globals are a bad idea. Anyone reading this code says "Where the hell did deduction come from?" Then they have to go look at all your globals to see if it is one. This isn't such a big deal in a small project, but it's annoying, and in a bigger program this would quickly become a nightmare.

    I'll skip the rest of the functions, since the issue is similar in all of them.
    Code:
    int main ()
    {
         /*Execute functions*/
         get_hours (hours);
         calculate_overtime (overtime);
         calculate_gross (gross);
    	 calculate_deduction (deduction);
    	 calculate_net (net_pay);
         output ();
    
         printf ("\n");
         /*Keep window open*/
         system("PAUSE");
    }
    Looking at the main function, none of your functions need to return anything, since you ignore the return value of all of them. Make them all void.

    Ideally, you'd want to skip all of the globals, and actually use the return value for something. But for now, just take a look at the comments above and see what that gets you.

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

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    9

    Thumbs up

    Hei many,many thanks,

    Finaly I get it to "work", noe I have to do the same program by using 2D array. Any advise how I can do that without pain!!!!

    Code:
    #include <stdio.h>
    
    
    	 
    
    float wage[5]={10.60, 9.75, 10.50, 12.25, 8.35};
    float hours[5];
    float overtime[5];
    float gross[5];
    float deduct[5];
    float net_pay[5];
    long id_number[5]={5001,5002,5003,5004,5005};
    
    
    float get_hours (float x)
    {
         int i;     
         
    
         for (i=0; i<5; ++i)
         {
             printf ("Enter employee #%d hours worked: ",id_number[i]);
             scanf ("%f", &hours[i]);
        
         }
         return (x);
    }
    
    float calculate_overtime (float x)
    {
         int i ;
    	 
         for (i=0; i<5; ++i)
         {
             if (hours[i] > 40)
                 overtime[i] = hours[i] - 40;
             else
                 overtime[i] = 0;
         }
         return (x);
    }
    
    float calculate_gross (float x)
    {
         int i ;
         
         for (i=0; i<5; ++i)
         {
             if  (overtime[i] > 0)
                 gross[i] = (wage[i] * 40) +
                 ((wage[i] * 1.5) * overtime[i]);
             else
                 gross[i] = (wage[i] * hours[i]) +
                 ((wage[i] * 1.5) * overtime[i]);
         }
         return (x);
    }
    
    float calculate_deduct (float x)
    {
         int i ;
    	 
         for  (i=0; i<5; ++i) 
    		 
    		deduct[i]=gross[i]/3.0;
    	    
          return (x);
    }
    
    float calculate_net_pay (float x )
    {
         int i ;
    	 
         for (i=0; i<5; ++i)
         {
             
    		net_pay[i]=gross[i] - deduct[i];
    
         }
         return (x);
    }
    
    
    void output (void)
    {
         int i;
    
         printf ("\n\n");
         printf ("--------------------------------------------------------------------\n");
         printf ("EM#    Pay Rate   Hours      OT       Gross Pay    Deduct   Net Pay  \n");
         printf ("--------------------------------------------------------------------\n");
    
         for (i=0; i<5; ++i)
         {
              printf ("%i     $%5.2f      %5.1f     %5.1f     $%6.2f  $%6.2f   $%6.2f\n",
             id_number[i], wage[i], hours[i],
              overtime[i], gross[i],deduct[i],net_pay[i]);
         }
    }
    
    
    int main ()
    {
         float x;
         get_hours (x);
         calculate_overtime (x);
         calculate_gross (x);
         calculate_deduct(x);
    	 calculate_net_pay (x);
         output ();
    
         printf ("\n");
         system("PAUSE");
    }
    
    :D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. convert 2D array into 1D array
    By coolvik87 in forum C Programming
    Replies: 4
    Last Post: 08-01-2006, 09:51 AM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM