Thread: How do we add across an array?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    45

    How do we add across an array?

    I have a text file that have the following data,

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

    I have put those values into a one dimension array already using a loop and infile >> Line0 >> Line1 >> Line2 >> ....

    So for example Line0[0] would be 0. Line0[1] would be 11. Line0[2] would be 18. Line1[1] would be 1 and so forth.

    What my question is how do we use a loop to add up the sum of the top row(0 1 2 3 4 5 6 7 8 9 10)?

    Is there a method to change part of the variable name? I am thinking of something like this:
    int i;
    i=0;
    Linei[0].
    From there I just create a simple loop to keep adding the sum.

    I know the above code will give you an undeclared variable but is it possible to do that? Or should I be looking for another method? Thanks.
    Last edited by chickenlittle; 04-25-2011 at 02:09 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You mentioned "one dimension array". Why not use a two dimensional array?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by chickenlittle View Post
    Is there a method to change part of the variable name? I am thinking of something like this:
    int i;
    i=0;
    Linei[0].
    No, but obviously if you change that part of the name "i" to an array, it easily becomes Line[i][0] which is doable. That is, a 2D array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    Wouldn't I have to use two loops in order to enter in the data with a 2-D array? The data I am getting from is from a textfile. I'm not sure how I would enter that data into a 2-D array.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    *shrug* How is it different from having LineX where X is a number?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    The problem I am having is how do we add numbers across the row by using a loop?
    Code:
    int sum;
    sum = 0;
    for(int i = 0; i < 3; i++)
    	{
    		for(int j = 0; j < 11; j++)
    		{
    		sum = sum + num[i][j];
    		}
    
    	}
    Last edited by chickenlittle; 04-25-2011 at 02:26 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What is wrong with your current approach?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  2. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  3. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM