array calculations

This is a discussion on array calculations within the C Programming forums, part of the General Programming Boards category; I have an array of strings, which allow a user to enter a name for the staff member, the amount ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    12

    array calculations

    I have an array of strings, which allow a user to enter a name for the staff member, the amount they get paid and the hours they have worked. I want to multiply the pay by the hours (pay * hours, eg. $15.90 x 21hrs). I am confused on how to do this. The code I have so far is below.

    Code:
    #include<stdio.h>
    int main (void)
    {
    	/*declare variables */
    	int index1, index2;
    	char staff[20][3][50];
    
    	/*Allow a user to enter staff's details*/
    	printf("Please enter the staff's name, pay classification (in $) and hours worked.\n");
    				
    		for (i = 0; i < numRows; i++)
    		{
    			for (j = 0; j < numCols; j++)
    			{ 						
    		 	           scanf("%s", staff[i][j]);
    			}/*end (for (j = 0; j < numCols; ++j))*/
    
    			putchar('\n');
    					
    		}/*end (for (i = 0; i < numRows; ++i))*/
    
    printf("\n\n Name: %s, Pay class: %s, Hrs: %s\n", staff[0][0], staff[0][1], staff[0][2]);
    	
         return 0;
    }

  2. #2
    Registered User ortegac's Avatar
    Join Date
    Mar 2006
    Location
    In front of the computer screen
    Posts
    15

    Check to see if it helps

    //Hey iam new in c programming but check if this can help you out. I //did not use no array only to store the name. The rest is to store //float values. It works.

    //I have an array of strings, which allow a user to enter a name for the staff member,
    //the amount they get paid and the hours they have worked. I want to multiply the pay by the hours (pay * hours, eg. $15.90 x 21hrs).
    //I am confused on how to do this. The code I have so far is below.

    Code:
    #include<stdio.h>
    main()
    {
    	char name[20];    // variable for name. array of 20 characters
    	float amtpayed;     // variable amount payed
    	float hrworked;     // hours worked
    	float totalpayed;   // total payed ( pay * hours)
    
    	printf("Please enter your name? (and press enter)\n");
    	scanf("%s",&name); // gets and store the name in the variable
    
    	printf("What is the amount $ you get payed? (and press    enter)\n");
    	scanf("%f",&amtpayed); // gets and store the value in the variable
    
    	printf("What is the amount of hours worked? (and press enter)\n");
    	scanf("%f",&hrworked); // gets and store the value in the variable
    
    	totalpayed = amtpayed * hrworked;  // formula to get totalpayed
    	
    	printf("\n* Name is: %s * Total amount payed is: %0.2f dollars\n\n", name, totalpayed);  // print results
    	return 0;
    }

  3. #3
    Registered User ortegac's Avatar
    Join Date
    Mar 2006
    Location
    In front of the computer screen
    Posts
    15

    Tell me if this help you

    I think you are trying to do a multidimension array. But what you really need is a structure. You are using an array for the name which is fine. But for the pay and amount. You need to use float. Check your data types.

    Tell me if this help you

    Caleb

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    12
    The only problem with structures is that I havent learnt them yet
    I know how to set one up
    Code:
    struct Person {
    char name[20];
    float pay[20];
    float hrs [20];
    };
    Person person[10];
    But I do not know how to add data and sort through it yet.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    hey try to use the variable totalpayed as double and then replace your present code of multiplication with the one below and test

    totalpayed = (double) amtpayed * hrworked; // formula to get totalpayed

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    scanf("%s", staff[i][j]);
    Look up fgets().

    Why don't you just use a array of doubles (or floats)?

    Code:
    #include<stdio.h>
    int main (void)
    {
    	/*declare variables */
    	int i, j;
    	double staff[20][3][50];
    
    	/*Allow a user to enter staff's details*/
    	printf("Please enter the staff's name, pay classification (in $) and hours worked.\n");
    				
    		for (i = 0; i < 20; i++)
    		{
    			for (j = 0; j < 3; j++)
    			{ 						
    		 	           scanf("%lf", staff[i][j]);
    			}/*end (for (j = 0; j < numCols; ++j))*/
    
    			putchar('\n');
    					
    		}/*end (for (i = 0; i < numRows; ++i))*/
    
    printf("\n\n Name: %s, Pay class: %s, Hrs: %s\n", staff[0][0], staff[0][1], staff[0][2]);
    	
         return 0;
    }
    Something like that might work . . . if you fixed the printf() . . . .
    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.

  7. #7
    Registered User ortegac's Avatar
    Join Date
    Mar 2006
    Location
    In front of the computer screen
    Posts
    15

    Solution is to use a simple structure

    Here is in use of a simple structure. This is a structure that contains array members. You can only use the array of strings in the variables fname and lname.
    To be more accurate you need to have a double, as enggabhinandan said. I see that dwk is helping you out too. But I donít know if that will work, if he says so he knows what he is doing. It might work too.

    Tell me if this help you out!
    Caleb

    Code:
    /* Demonstrates a structure that has array members. */
    
    #include <stdio.h>
    
    /* Define and declare a structure to hold the data. */
    /* It contains one float variable and two char arrays. */
    
      struct data{
          char fname[30];
          char lname[30];
          double pay;
          double hrs;
          double totalpayed;
     } rec;
    
      main()
    {
         /* Input the data from the keyboard. */
    
         printf("Enter the Person first and last names?,\n");
         printf("separated by a space: ");
         scanf("%s %s", rec.fname, rec.lname);
    
         printf("\nEnter the $ amount payed per hour?: ");
         scanf("%lf", &rec.pay);
    
    	 printf("\nEnter the amount of Hours worked?: ");
         scanf("%lf", &rec.hrs);
    
    	 rec.totalpayed = rec.pay * rec.hrs;
    
         /* Display the information. */
         /* Note: %.2f specifies a floating-point value */
         /* to be displayed with two digits to the right */
         /* of the decimal point. */
    
         /* Display the data on the screen. */
    
         printf("\nPerson Name: %s %s Pay: $%.2lf Hrs: %.2lf Total pay: $%.2lf\n", rec.fname, rec.lname, rec.pay, rec.hrs, rec.totalpayed);
    
         return 0;
    }

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    12
    This is what I have so far

    Code:
    #include<stdio.h>
    
    #define numRows 10 /* number of rows */
    
    float payCalculation(struct staff staff[numRows]);
    
    struct staff
    { 
    	char name[50];
    	float payClassification;
    	float hoursWorked;
    };
    
    int main (void)
    {
    	struct staff staff[10];
    
    for (i = 0; i < numRows; i++)
    {
    	printf("\nPlease enter the staff's name: ");
    	fgets(employee[i].name, sizeof(staff[i].name), stdin);
    	printf("\nPlease enter the staff's pay classification: ");
    	fscanf(stdin, "%f", &staff[i].payClassification);
    	printf("\nPlease enter the staff's hours worked: ");
    	fscanf(stdin, "%f", &staff[i].hoursWorked);					
    }/*end (for (i = 0; i < numRows; ++i))*/
    
    printf("Name: %s Pay: payCalculation(employee[i])", employee[i].name);
    
    	return 0;
    }/*end main() function */
    
    float payCalculation(struct staff staff[numRows])
    {
    	int i;
    	float pay =  staff[i].payClassification *staff[i].hoursWorked;
            return pay;	
    }
    I am using the function to calculate the pay for each individual employee but I think I have done something wrong in setting up the function and printing out the pay.

    Also in my for loop i am unable to enter data for the staffs name. It prints the string asking for the name but does not allow me to enter any data instead it skips and asks me to enter the pay classification is there away i can stop it skipping?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    float payCalculation(struct staff staff[numRows])
    {
    	int i;
    	float pay =  staff[i].payClassification *staff[i].hoursWorked;
            return pay;	
    }
    i is unintialized.
    Code:
    user@0[~]$ gcc -W -Wall -ansi -pedantic -O2 -s -o t t.c
    t.c:5: warning: array type has incomplete element type
    t.c:5: warning: `struct staff' declared inside parameter list
    t.c:5: warning: its scope is only this definition or declaration, which is probably not what you want
    t.c: In function `main':
    t.c:18: error: `i' undeclared (first use in this function)
    t.c:18: error: (Each undeclared identifier is reported only once
    t.c:18: error: for each function it appears in.)
    t.c:21: error: `employee' undeclared (first use in this function)
    t.c: At top level:
    t.c:34: error: conflicting types for `payCalculation'
    t.c:5: error: previous declaration of `payCalculation'
    t.c:38:2: warning: no newline at end of file
    user@0[~]$
    I would guess you'll want to pass i in from main() into payCalculation().
    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. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Calculations on an entered array
    By MV1 in forum C Programming
    Replies: 7
    Last Post: 03-10-2009, 10:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21