Thread: array calculations

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

  2. #2
    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;
    }

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