Hard time getting an array to run

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 838 Views - Last Post: 10 February 2012 - 02:27 PM Rate Topic: -----

Topic Sponsor:

#16 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 3756
  • View blog
  • Posts: 9,707
  • Joined: 16-October 07

Re: Hard time getting an array to run

Posted 08 February 2012 - 07:03 AM

Parallel arrays, never a good idea.

Use a struct. Use loops. But don't use a look loop for (int i=0; i < 1; i++) because, honestly, what's the point?

e.g.
#include <iostream>

using namespace std;

const int RUNNER_DAYS = 7;
struct Runner {
	const char *name;
	int milesDay[RUNNER_DAYS];
};

void showRunner(Runner &item) {
	cout << item.name << endl;
	cout << "   ";
	for(int i=0; i<RUNNER_DAYS; i++) {
		cout << ' ' << item.milesDay[i];
	}
	cout << endl;
}

int main() {
	const int runnersCount = 5;
	Runner runners[runnersCount] = {
		{ "jason", {1,2,3,4,5,6,7} },
		{ "samantha", {2,4,6,8,10,12,14} },
		{ "ravi", {3,6,9,12,15,18,21} },
		{ "sheila", {1,1,4,4,7,8,10} },
		{ "ankit", {5,5,5,5,5,5,5} },
		
	};
	for (int i=0; i<runnersCount; i++) {
		showRunner(runners[i]);
	}
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#17 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 09-March 11

Re: Hard time getting an array to run

Posted 08 February 2012 - 09:11 AM

Wow, thanks for that insight. I was doing triple the work I should have been doing. So basically you took [numRunners][maxNameSize] and that is replacing the [row][column].

I went ahead and I made the first index milesDay[i] and it did not work. I tried changing the 'for' loop to a for (int p=0; p < milesDay; ++p) and tried p++ and that did not work. I have been trying multiple combinations to no avail.

#include <iostream>
#include <iomanip>

using namespace std;

// Constants defined for later use
const int numRunners = 5;
const int numRaces = 7;
const int maxNameSize = 20;


int main()
{

int milesDay[5][7]={{1,2,3,4,5,6,7},{2,4,6,8,10,12,14},{3,6,9,12,15,18,21},{1,1,4,4,7,8,10},{5,5,5,5,5,5,5}};
char runnerName[numRunners][maxNameSize]={"Jason","Samantha","Ravi","Sheila","Ankit"};
const int numRunners = 5;
int sum;
double average;

    for (int i=0; i < numRunners; ++i)
{
    cout << runnerName[i] <<endl;

        for (int i=0; i < milesDay; ++i)
    {
        cout << milesDay[i]<< endl;
    }

}


    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#18 shurd  Icon User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 113
  • Joined: 05-February 12

Re: Hard time getting an array to run

Posted 08 February 2012 - 09:28 AM

milesDays is an array not an int. You want to do:
for(int i=0;i<numRunners;i++)
{
    for(int p=0;p<numRaces;p++)
    {
        cout<<milesDay[i][p];
    }
}

Was This Post Helpful? 0
  • +
  • -

#19 jimblumberg  Icon User is offline

  • member icon

Reputation: 1892
  • View blog
  • Posts: 5,681
  • Joined: 25-December 09

Re: Hard time getting an array to run

Posted 08 February 2012 - 09:31 AM

There is a difference between C-strings (null terminated character arrays) and multidimensional int arrays. When dealing with your milesDay array you have two dimensions. The first dimension, for your example is related to the runnersName, the second dimension contains the actual miles.

When you see milesDay[0], you are seeing the address to the start of the array containing runner 1's miles per day. The first group in your declaration {1,2,3,4,5,6,7}.

milesDay[0][0] == 1
milesDay[0][1] == 2
milesDay[0][2] == 3
milesDay[0][3] == 4
milesDay[0][4] == 5
milesDay[0][5] == 6
milesDay[0][6] == 7

Your second loop is in the correct location but it should be using a different variable name than the first loop, maybe something like j, instead of i. This variable will be used for the second index of your array. Something like milesDay[runnerNameIndex][milesPerDayIndex], where runnerNameIndex is the number of the runner you are printing, and milesPerDayIndex is the actual daily miles per day that the runner ran.

I agree with baavgai that using a structure to hold this information would make things much cleaner and the logic simpler.


Jim
Was This Post Helpful? 0
  • +
  • -

#20 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 09-March 11

Re: Hard time getting an array to run

Posted 08 February 2012 - 11:10 AM

I changed the variable from i to j, and also went ahead and initialized 'runnerNameIndex' and 'milesPerDayIndex' to be used with the milesDay array.

i tried it with two separate ways with no go.. milesDay[0] and milesDay[j]. This is very confusing!!

I agree that structs would make this clearer and more logical, but I would also have to include that may get me to be 100% more confused than where I am now. I am definately making progress however, which I do appreciate. I am here now....

#include <iostream>
#include <iomanip>

using namespace std;

// Constants defined for later use
const int numRunners = 5;
const int numRaces = 7;
const int maxNameSize = 20;
const int runnerNameIndex = 5;
const int milesPerDayIndex = 3;

int main()
{

int milesDay[runnerNameIndex][milesPerDayIndex]={{1,2,3,4,5,6,7},{2,4,6,8,10,12,14},{3,6,9,12,15,18,21},{1,1,4,4,7,8,10},{5,5,5,5,5,5,5}};
char runnerName[numRunners][maxNameSize]={"Jason","Samantha","Ravi","Sheila","Ankit"};
const int numRunners = 5;
int sum;
double average;

    for (int i=0; i < numRunners; ++i)
{
    cout << runnerName[i] <<endl;

        for (int j=0; j < milesPerDayIndex; ++j)
    {
        cout << milesDay[j]<< endl;
    }

}


    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#21 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 09-March 11

Re: Hard time getting an array to run

Posted 08 February 2012 - 11:55 AM

I have made some progress however not in the correct context. I am able to get each of the miles to run, however they are running vertically and not horizontally. The main issue lies with the fact that it is showing all of the miles ran for each of the runners separately. So Runner (1) is showing the run times for runner (1 - 5) instead of just himself.
#include <iostream>
#include <iomanip>

using namespace std;

// Constants defined for later use
const int numRunners = 5;
const int numRaces = 7;
const int maxNameSize = 20;
const int runnerNameIndex = 5;
const int milesPerDayIndex = 7;

int main()
{

int milesDay[runnerNameIndex][milesPerDayIndex]={{1,2,3,4,5,6,7},{2,4,6,8,10,12,14},{3,6,9,12,15,18,21},{1,1,4,4,7,8,10},{5,5,5,5,5,5,5}};
char runnerName[numRunners][maxNameSize]={"Jason","Samantha","Ravi","Sheila","Ankit"};
const int numRunners = 5;
int sum;
double average;

    for (int i=0; i < numRunners; ++i)
{
    cout << runnerName[i] <<endl;

        for (int j=0; j < runnerNameIndex; ++j)
        for (int m=0; m < milesPerDayIndex; ++m)
    {
        cout << milesDay[j][m]<< " " <<endl;
    }

}


    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#22 jimblumberg  Icon User is offline

  • member icon

Reputation: 1892
  • View blog
  • Posts: 5,681
  • Joined: 25-December 09

Re: Hard time getting an array to run

Posted 08 February 2012 - 12:11 PM

You only need 2 for loops not 3. Remember the arrays are related. Each runner has a certain number races (number of miles run?).

Also the following:
// Constants defined for later use
const int numRunners = 5;
const int numRaces = 7;
const int maxNameSize = 20;
const int runnerNameIndex = 5;
const int milesPerDayIndex = 7;

int main()
{

int milesDay[runnerNameIndex][milesPerDayIndex]={{1,2,3,4,5,6,7},{2,4,6,8,10,12,14},{3,6,9,12,15,18,21},{1,1,4,4,7,8,10},{5,5,5,5,5,5,5}};
char runnerName[numRunners][maxNameSize]={"Jason","Samantha","Ravi","Sheila","Ankit"};


Could be simplified to:
// Constants defined for later use
const int numRunners = 5;
const int numRaces = 7;
const int maxNameSize = 20;

int main()
{

int milesDay[numRunners][numRaces]={{1,2,3,4,5,6,7},{2,4,6,8,10,12,14},{3,6,9,12,15,18,21},{1,1,4,4,7,8,10},{5,5,5,5,5,5,5}};
char runnerName[numRunners][maxNameSize]={"Jason","Samantha","Ravi","Sheila","Ankit"};


Do you see the relationship between these two arrays?

Jim
Was This Post Helpful? 1
  • +
  • -

#23 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 09-March 11

Re: Hard time getting an array to run

Posted 08 February 2012 - 01:20 PM

Jim you are a true master. I was able to figure it out based on what you said by not having to need the additional 3rd loop. This also caused me to figure out why it was outputting the code vertically and not horizontally. Thanks!

I want to put in a third 'for' loop in to this in order to calculate the total amount of miles and the averages of them. What I input looks correct to me, however again not running.

#include <iostream>
#include <iomanip>

using namespace std;

// Constants defined for later use
const int numRunners = 5;
const int numRaces = 7;
const int maxNameSize = 20;


int main()
{


int milesDay[numRunners][numRaces]={{1,2,3,4,5,6,7},
                                    {2,4,6,8,10,12,14},
                                    {3,6,9,12,15,18,21},
                                    {1,1,4,4,7,8,10},
                                    {5,5,5,5,5,5,5}};
char runnerName[numRunners][maxNameSize]={"Jason","Samantha","Ravi","Sheila","Ankit"};
const int numRunners = 5;
int sum;
double average;

    for (int i=0; i < numRunners; ++i)

{
    cout << endl;
    cout << runnerName[i] << endl;
    cout << "Miles Ran: "<< endl;

        for (int m=0; m < numRaces; ++m)
{
    cout << milesDay[i][m] << "  ";


            for (j=0; j < milesDay; ++j)
{
        sum = sum + milesDay[i][m];
        average = sum / 7 << endl;
    cout << sum <<endl;
    cout << average << endl;
}

}

}


    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#24 jimblumberg  Icon User is offline

  • member icon

Reputation: 1892
  • View blog
  • Posts: 5,681
  • Joined: 25-December 09

Re: Hard time getting an array to run

Posted 08 February 2012 - 02:09 PM

You don't need the third loop. Just total the miles ran in the second loop, do the average calculation after the second loop ends (but before the first loop ends) and print the results. Remember to zero out this sum between runners.

Also a little better indentation would help you with your logic.

Look at the comments in the code.

#include <iostream>
#include <iomanip>

using namespace std;

// Constants defined for later use
const int numRunners = 5;
const int numRaces = 7;
const int maxNameSize = 20;


int main()
{


   int milesDay[numRunners][numRaces]= {{1,2,3,4,5,6,7},
                                        {2,4,6,8,10,12,14},
                                        {3,6,9,12,15,18,21},
                                        {1,1,4,4,7,8,10},
                                        {5,5,5,5,5,5,5} };
   char runnerName[numRunners][maxNameSize]= {"Jason",
                                              "Samantha",
                                              "Ravi",
                                              "Sheila",
                          
                                              "Ankit"};
   // const int numRunners = 5; // This line is not needed already defined above.
   
   int sum;
   double average;

   for (int i=0; i < numRunners; ++i)
   {
      cout << endl;
      cout << runnerName[i] << endl;
      cout << "Miles Ran: "<< endl;

      for (int m=0; m < numRaces; ++m)
      {
         cout << milesDay[i][m] << "  ";
         
         // Do the sum here.

         for (j=0; j < milesDay; ++j)
         {
            sum = sum + milesDay[i][m];
            average = sum / 7 << endl;
            cout << sum <<endl;
            cout << average << endl;
         }
      }
      // Do the average calculation and print here. 
   }

   return 0;
}




Jim
Was This Post Helpful? 0
  • +
  • -

#25 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 09-March 11

Re: Hard time getting an array to run

Posted 08 February 2012 - 08:00 PM

Hm. I put the calculation in, not allowing a run.
#include <iostream>
#include <iomanip>

using namespace std;

// Constants defined for later use
const int numRunners = 5;
const int numRaces = 7;
const int maxNameSize = 20;


int main()
{


int milesDay[numRunners][numRaces]={{1,2,3,4,5,6,7},
                                    {2,4,6,8,10,12,14},
                                    {3,6,9,12,15,18,21},
                                    {1,1,4,4,7,8,10},
                                    {5,5,5,5,5,5,5}};
char runnerName[numRunners][maxNameSize]={"Jason","Samantha","Ravi","Sheila","Ankit"};
const int numRunners = 5;
int sum;
double average;

    for (int i=0; i < numRunners; ++i)

{
    cout << endl;
    cout << runnerName[i] << endl;
    cout << "Miles Ran: "<< endl;

        for (int m=0; m < numRaces; m++)
{
    cout << milesDay[i][m] << "  ";
          
	sum = 0;
    sum = sum + milesDay[m] << " ";
    cout << sum <<endl;

	

}
	average = sum / 7;
    cout << average << endl;
}
	system("pause");
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#26 jimblumberg  Icon User is offline

  • member icon

Reputation: 1892
  • View blog
  • Posts: 5,681
  • Joined: 25-December 09

Re: Hard time getting an array to run

Posted 08 February 2012 - 08:09 PM

Please post your error messages exactly as they appear in your development environment.

You need to study very carefully this link: Arrays. If there is something in that link you do not understand, show the section and ask specific questions.


Jim
Was This Post Helpful? 0
  • +
  • -

#27 shurd  Icon User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 113
  • Joined: 05-February 12

Re: Hard time getting an array to run

Posted 09 February 2012 - 03:02 AM

sum = sum + milesDay[m] << " ";


You forgot to use the i here,
sum = sum + milesDay[i][m] << " ";


And the sum =0 has to be before this line
for (int m=0; m < numRaces; m++)


And the sum =0 has to be before this line
for (int m=0; m < numRaces; m++)

Was This Post Helpful? 1
  • +
  • -

#28 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 09-March 11

Re: Hard time getting an array to run

Posted 10 February 2012 - 01:58 PM

In trying to understand Arrays more effectively I have a quick question about them.

Why did you list constants before the int main() ?

It would not have worked after int main()?

I read why we have to declare constants. Does this mean every Array requires a constant declared?

So for example..

double testArray[20][30]

In an actual program it must be listed as….

const double testRow = 20;
const double testColumn = 30; 

Int main()
{
double testArray[testRow][testColumn] = {1,2,3,4,5,6,7,,8…..},{2,3,4,5,6,…..etc} etc etc




Is this correct in saying?
Was This Post Helpful? 0
  • +
  • -

#29 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 438
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Hard time getting an array to run

Posted 10 February 2012 - 02:06 PM

View Postxkaijinx, on 10 February 2012 - 08:58 PM, said:

In trying to understand Arrays more effectively I have a quick question about them.

Why did you list constants before the int main() ?

It would not have worked after int main()?


Constants must be declared before they are used. If you need them in main, you have to declare them before or in main. Usually people put them somewhere on the top because it is something you might change quite a lot.

Quote

I read why we have to declare constants. Does this mean every Array requires a constant declared?


It's not required to declare constants for array sizes but it is recommended. This constant is the size of the array and you are going to need it more than once. For looping through the array perhaps, to pass the size to some function, to check if you are not reading past the bounds of an array. By making it a constant, you guarantee that you get the size right every time and you can easily change the size by changing it in one place and all your other code will still work.

In general it is recommended to make any value, you use more than once, a constant.

This post has been edited by Karel-Lodewijk: 10 February 2012 - 02:07 PM

Was This Post Helpful? 0
  • +
  • -

#30 shurd  Icon User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 113
  • Joined: 05-February 12

Re: Hard time getting an array to run

Posted 10 February 2012 - 02:27 PM

@xkaijinx - Creating constants is just to make changes easier, say you have a 5 spaces long array and you want to change it to 8. If you had a constant you could simply change one line, but if you don't you will have to change all the lines where you used the old size.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2