Like Tree1Likes
  • 1 Post By quzah

2d arrays

This is a discussion on 2d arrays within the C Programming forums, part of the General Programming Boards category; how to print a specific number in the array? Like for my program I have to calculate the cost of ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    1

    2d arrays

    how to print a specific number in the array? Like for my program I have to calculate the cost of 5 companies in 4 years. For the last function how do I let say print the total cost of company 1.

    Code:
    #include <stdio.h>
    void getvalues();
    double totalcost(float);
    void main()
    {
    	
    	getvalues();
        y=double totalcost(cost);
    	
    	
    }
    
    void getvalues()
    {
    	float cost[5][6];
    	int i,y,com=0,year=0;
    	for(i=0;i<5;i++)
    	{
    		for(y=0;y<4;y++)
    		{
    		com=i+1;
    		year=y+1;
    		printf("Please enter the electricity cost for Company %d, year %d:",com,year);
    		scanf("%f",&cost[i][y]);
    		}
    	}
    }
    	double totalcost(float c)
    	{
    	int selection;
    	printf("Please enter the number of company");
    	scanf("%d",selection);

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to figure out who "owns" your data. Currently the getvalues function owns your cost data, which means that it is fruitless for any other function (like main, or totalcost) to even attempt to dream about the data, let alone use it in a meaningful way.

    It is typical for your data to be "owned" by the main program, which passes it to functions as necessary for them to operate on. So you should put your array in main, pass it to getvalues to be filled in, and then pass it to totalcost to be calculated with.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use variables that make sense, and you will have your answer:
    Code:
    float costs[ 5 ][ 6 ];
    int cost, company;
    
    printf( "Which company (0-4)?\n" );
    scanf( "%d", &company )
    if( company > -1 && company < COMPANIES )
        for( cost = 0; cost < COSTS; cost++ )
            dosomething( costs[ company ][ cost ] );
    Now it doesn't actually matter if you treat "rows" as the company, or the "columns" as the company. You can do whatever you want. Just stick with whatever you choose throughout your program and don't flip-flop back and forth.

    Quzah.
    Salem likes this.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  3. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  4. Parallel Arrays with Multiple Arrays
    By Billye Scott in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 11:14 PM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 01:43 AM

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