Thread: I need C program uses a double-subscript array

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    6

    Thumbs down I need C program uses a double-subscript array

    Hi ..
    can you help me with this :



    Code:
    
    Write a C program which uses a double-subscript array named
    
    
    employeeSalary in the following methods:
    
    
     total (this method should returns the sum of all salaries)
    
    
     mean (this method should returns the average of all salaries)
    
    
    Initialize the employeeSalary array with the following values:
    
    
    employeeSalary = { {50, 100, 25, 33}, 
    
    
     {42, 52, 65, 73}, 
    
    
     {89, 44, 17, 18}};
    

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Can we help you with this? Nope. Not allowed. This site has a homework policy that requires you to demonstrate genuine effort before others will help you.

    Genuine effort does not refer to the effort to copy/paste your homework exercise into a forum post.

    So, what have you tried?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    6
    please can you help now ..



    Code:
    #include <stdio.h>
    #define EMP 3
    #define SAL 4
    
    
    
    
    int mean (const int setOfsalarys[],size_t tests);
    void printArray(int employee [] [SAL]);
    
    
    int main(void)
    {
    	size_t m ;
    
    
    	int employeeSalary[EMP][SAL]=
    	       { {50, 100, 25, 33}
    	        ,{42, 52, 65, 73}
    	       , {89, 44, 17, 18}};
    
    
    	puts("the values in array are :");
    	printArray( employeeSalary , EMP , SAL );
    
    
    	for (m = 0 ;m <= EMP ; ++ m){
    	printf("mean =%d\n" ,mean( employeeSalary [m] , SAL));
    }
    
    
    		
    }
    int mean (const int setOfsalarys[])
    {
      
    size_t k;
    int total = 0;
    
    
    for (k = 0 ; k <=12 ; ++k ){
    total += setOfsalarys[k];
    }
    
    
    return ( int ) total / 12;
    }
    
    
        void printArray(int employee [] [SAL])
    	{
    
    
    	size_t i;
    	size_t j;
    
    
    	for ( i = 0 ; i <= 4 ; ++i) {
    
    
    		for ( j = 0 ; j <= 3 ; ++j) {
    
    
    			printf("%d  ",employee[ i ][ j ]);
    		}
    
    
    		printf("\n");
    	}
    	}

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Read the third link in my signature.

    Yes, I can see plenty of concerns in your code. However, you will learn more if you try giving information about what the code is doing versus what you expect.

    The compiler is rejecting your code, right? When it does that, it gives diagnostic messages that explain why. You need to make an attempt to understand those messages - as you might then be able to work out the solution of your problem for yourself. If you can't work out the solution, then telling us what the compile complains about will help us better help you.

    Once you've got the code to compile, there are more problems - it will not produce the results you intend. In that case, you need to give us information about what results you expect versus what you get.


    It may seem like I'm being difficult but software development is about problem solving. If we just spoon out solutions, you will learn nothing that improves your problem solving skills. Which is part of the reason for this site's homework policy.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for ( i = 0 ; i <= 4 ; ++i)
    Try using your constants EMP and SAL.

    Also, as a rule, you use < and not <= when subscripting through an array.

    After that, resolve whether you intend to call mean() with one or two parameters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2014
    Posts
    6
    Thank you salem and grumpy .

    I knew the solution now ..

    This is my code


    Code:
    #include <stdio.h>
    #define EMPLOYEE 3
    #define SALARY 4
    
    
    
    
    int mean (const int setOfsalarys[],size_t tests);
    double total (const int setOfsalarys[],size_t tests);
    void printArray(int employee [] [SALARY] ,size_t pupils,size_t tests );
    
    
    int main(void)
    {
    	size_t employee ;
    
    
    	int employeeSalary[EMPLOYEE][SALARY]=
    	       { {50, 100, 25, 33}
    	        ,{42, 52, 65, 73}
    	       , {89, 44, 17, 18}};
    
    
    	puts("the values in array are :\n");
    	printArray( employeeSalary , EMPLOYEE , SALARY );
    
    
    	for (employee = 0 ;employee < EMPLOYEE ; ++ employee){
    	printf("\nthe mean for employee %u is %.2d\n" ,employee ,mean( employeeSalary [employee],SALARY) );
    	printf("\nthe total for employee %u is %.2f\n" ,employee ,total( employeeSalary [employee],SALARY) );
    }
    }
    
    
    int mean (const int setOfsalarys[],size_t tests)
    {
      
    size_t k;
    int total = 0;
    
    
    for (k = 0 ; k < tests ; ++k ){
    total += setOfsalarys[k];
    }
    
    
    return ( int ) total / tests;
    }
    
    
    
    
    
    
    double total (const int setOfsalarys[],size_t tests) 
    {
    
    
    
    
    size_t k;
    int total = 0;
    	  
    for (k = 0 ; k < tests ; ++k ){
    	total += setOfsalarys[k];
    
    
    }
    return ( double ) total;
    }
    
    
    void printArray(int employee [] [SALARY], size_t pupils, size_t tests )
    
    
    	{
    
    
    	size_t i;
    	size_t j;
    
    
    
    
    	printf("%s","                   [0]    [1]    [2]    [3]");
    
    
    	for ( i = 0 ; i < pupils ; ++i) {
    
    
    		printf("\n employeeSalary [%d] ",i );
    
    
    
    
    		for ( j = 0 ; j < tests ; ++j) {
    
    
    			printf("%-5d  ",employee[ i ][ j ]);
    		}
    	}
    	}

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Great job.

    Now all you need is a little care over how the code looks (for other humans), and you're done.
    Indent style - Wikipedia, the free encyclopedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Invalid types 'double*[double]' for array subscript
    By Elvio Esposito in forum C++ Programming
    Replies: 2
    Last Post: 07-01-2013, 11:47 AM
  2. array subscript is not an integer
    By prathiksa in forum C Programming
    Replies: 8
    Last Post: 11-06-2012, 12:15 PM
  3. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 12:57 PM
  4. Array subscript
    By poovizhipanpa in forum C Programming
    Replies: 6
    Last Post: 01-08-2009, 12:45 PM
  5. warning: array subscript has type `char'
    By pc_doctor in forum C Programming
    Replies: 5
    Last Post: 12-08-2008, 03:03 PM

Tags for this Thread