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:

#1 xkaijinx  Icon User is offline

  • D.I.C Head

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

Hard time getting an array to run

Posted 07 February 2012 - 02:16 PM

I have a parallel array which is storing the names of each "runner".

I also included a two-dimensional array which is storing the number of miles each runner ran per day.
7 numbers so 7 days.

I am really just trying to get program to read the runners names and output their run times for that day.. having a real hard time, I think I am making progress from where I first started.

#include <iostream>

using namespace std; 

int main()
{
char runnerName[5][7]={"jason","samantha","ravi","sheila","ankit"};
double 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};



	for (int i=0; i<5; i++)
{

	cout << milesDay[5][7] << " "; 

}

	system("pause"); 
	return 0; 
}




Is This A Good Question/Topic? 0
  • +

Replies To: Hard time getting an array to run

#2 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 07 February 2012 - 02:57 PM

You will need a loop to print each runner's name and another loop to print each runner's times. I would suggest you start by printing each of your runner's names. Once you get that running you can then try to print the runner's scores under the name.



Jim
Was This Post Helpful? 0
  • +
  • -

#3 Bryston  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 122
  • Joined: 24-January 12

Re: Hard time getting an array to run

Posted 07 February 2012 - 02:58 PM

[quote]

char runnerName[5][7]={"jason","samantha","ravi","sheila","ankit"};


If I understand your initial requirement, this doesnt need to be a 2 dimensional array, and the type of the array is incorrect.


double 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};


There is something missing from this snip, it wont populate the 2 dimensional array properly.


	for (int i=0; i<5; i++)
{

	cout << milesDay[5][7] << " "; 

}


You start out right by looping through the players, but you need another loop to read the data in the milesDay array.
Was This Post Helpful? 0
  • +
  • -

#4 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 07 February 2012 - 03:05 PM

Quote

If I understand your initial requirement, this doesnt need to be a 2 dimensional array, and the type of the array is incorrect.

Since the OP is using C-strings (null terminated character arrays) it does need to be a multidimensional array. However the array sizes are incorrect for the constants that are to be stored in this array. In order to hold all the names properly the array should be char runnerName[5][9] to be able to hold the longest name "samatha" which is 8 characters plus the end of string character (9).

Jim
Was This Post Helpful? 1
  • +
  • -

#5 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 07 February 2012 - 03:14 PM

I was going to post what Jim just did and for the second array just initializa it like that
double 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};


Than you just need to run some loops to print everything in the screen, do not forget to only run the runnerName loop until you find the '\0' character, which is the final character for C-strings.
Was This Post Helpful? 0
  • +
  • -

#6 Bryston  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 122
  • Joined: 24-January 12

Re: Hard time getting an array to run

Posted 07 February 2012 - 03:28 PM

Not intending to hijack the OP's post but...

I just did it as
char* runnerName[5]={"jason","samantha","ravi","sheila","ankit"};
double 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}};


and ran it through a nested loop. Is that an incorrect solution?
Was This Post Helpful? 0
  • +
  • -

#7 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 301
  • View blog
  • Posts: 1,039
  • Joined: 31-December 10

Re: Hard time getting an array to run

Posted 07 February 2012 - 03:32 PM

What compiler did you use a C or C++ one?
Was This Post Helpful? 0
  • +
  • -

#8 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 07 February 2012 - 03:33 PM

Quote

Is that an incorrect solution?


Yes, and runnerName is still a multidimensional array in your code.

Jim

This post has been edited by jimblumberg: 07 February 2012 - 03:38 PM

Was This Post Helpful? 0
  • +
  • -

#9 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 07 February 2012 - 03:41 PM

I did this:
const char *runnerName[9]= {"jason","samantha","ravi","sheila","ankit"};
double 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};


not posting the rest of the code as to not give all the answers.
Was This Post Helpful? 0
  • +
  • -

#10 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 07 February 2012 - 07:38 PM

I am getting unhandled exceptions now with error codes on my post. Not really sure what I am doing wrong here. Also it cannot be [5][9] for samantha's characters because [5] represents that there are 5 rows ad the [7] represents the amount of columns. Here is where I am at now, without luck.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

	double 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};

	const char* runnerName[5][7]={"jason","samantha","ravi","sheila","ankit"};


    for (int i=0; i < 5; i++)
{
	cout << runnerName[5][7] << " " <<endl;
   
}

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

    system("pause");
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#11 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 07 February 2012 - 08:08 PM

Quote

Also it cannot be [5][9] for samantha's characters because [5] represents that there are 5 rows ad the [7] represents the amount of columns.


No, 5 represents the number of runners, 9 represents the maximum number of characters in the runner's name.

Also the following code:
cout << runnerName[5][7] << " " <<endl;
will probably cause your unhandled exceptions. Arrays in C start at 0 and stop at size - 1. Since you defined your array as runnerName[5][7] this line will try to access this array out of bounds. So lets put some variables into this runnerName[i][j]. The allowable values for i would be 0,1,2,3,4 and the allowable values of j would be 0,1,2,3,4,5,6.

Now lets take a look at what this array is actually holding.

runnerName[0] == jason
runnerName[1] == samantha
etc.

Lets look at runnerName[0].
runnerName[0][0] == 'j'
runnerName[0][1] == 'a'
runnerName[0][2] == 's'
runnerName[0][3] == 'o'
runnerName[0][4] == 'n'
runnerName[0][5] == '\0' This is the end of string character.

So this name fits into this array with a couple of characters to spare.

Now lets take a look at runnerName[1] more closely. Remember the maxim number of characters is 7.
runnerName[1][0] == 's'
runnerName[1][1] == 'a'
runnerName[1][2] == 'm'
runnerName[1][3] == 'a'
runnerName[1][4] == 'n'
runnerName[1][5] == 't'
runnerName[1][6] == 'h'
runnerName[1][7] == 'a'
runnerName[1][8] == '\0'

But as I stated earlier the valid range for this is 0 to 6 (7 characters). So this would be out of bounds of your array. You must have a size of at least 9 characters to hold this name.

Jim
Was This Post Helpful? 0
  • +
  • -

#12 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 07 February 2012 - 09:19 PM

Thanks Jim, that helped alot on runnerName. I am getting hung up on the milesDay...

runnerName is working fine! I am having issues when trying to run milesDay...

It is telling me it is being used without being initialized however I used it as a "const double" and tried it as a "double"

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

	const double 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};

	const char* runnerName[9]={"jason","samantha","ravi","sheila","ankit"};


    for (int i=0; i < 1; i++)
{
	cout << runnerName[0][0] << runnerName[0][1] << runnerName[0][2] << runnerName[0][3] << runnerName[0][4] << runnerName[0][5] <<endl;
	cout << runnerName[1][0] << runnerName[1][1] << runnerName[1][2] << runnerName[1][3] << runnerName[1][4] << runnerName[1][5] << runnerName[1][6] << runnerName[1][7] << runnerName[1][8] << runnerName[1][9] <<endl;
	cout << runnerName[2][0] << runnerName[2][1] << runnerName[2][2] << runnerName[2][3] << runnerName[2][4] <<endl;
	cout << runnerName[3][0] << runnerName[3][1] << runnerName[3][2] << runnerName[3][3] << runnerName[3][4] << runnerName[3][5] << runnerName[3][6]<< endl;
	cout << runnerName[4][0] << runnerName[4][1] << runnerName[4][2] << runnerName[4][3] << runnerName[4][4] << runnerName[4][5] <<endl;
	}

for (int i=0; i < 7; i++)
{
    cout << milesDay[0][0] << milesDay[0][1]<< milesDay[0][2] << milesDay[0][3] << milesDay[0][4]<< milesDay[0][5] << milesDay[0][6] << milesDay[0][7]<< endl;
	cout << milesDay[1][0] << milesDay[1][1]<< milesDay[1][2] << milesDay[1][3] << milesDay[1][4]<< milesDay[1][5] << milesDay[1][6] << milesDay[1][7]<< endl;
	cout << milesDay[2][0] << milesDay[2][1]<< milesDay[2][2] << milesDay[2][3] << milesDay[2][4]<< milesDay[2][5] << milesDay[2][6] << milesDay[2][7]<< endl;
	cout << milesDay[3][0] << milesDay[3][1]<< milesDay[3][2] << milesDay[3][3] << milesDay[3][4]<< milesDay[3][5] << milesDay[3][6] << milesDay[3][7]<< endl;
	cout << milesDay[4][0] << milesDay[4][1]<< milesDay[4][2] << milesDay[4][3] << milesDay[4][4]<< milesDay[4][5] << milesDay[4][6] << milesDay[4][7]<< endl;
}

    system("pause");
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#13 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 07 February 2012 - 09:41 PM

Actually, this seems to be more proper however it still does not run...

I separated the milesDay in the two-dimensional array in separate rows of curly brace, and made it an int milesDay.


#include <iostream>
#include <iomanip>

using namespace std;

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}};

	const char* runnerName[9]={"jason","samantha","ravi","sheila","ankit"};


    for (int i=0; i < 1; i++)
{
	cout << runnerName[0][0] << runnerName[0][1] << runnerName[0][2] << runnerName[0][3] << runnerName[0][4] << runnerName[0][5] <<endl;
	cout << runnerName[1][0] << runnerName[1][1] << runnerName[1][2] << runnerName[1][3] << runnerName[1][4] << runnerName[1][5] << runnerName[1][6] << runnerName[1][7] << runnerName[1][8] << runnerName[1][9] <<endl;
	cout << runnerName[2][0] << runnerName[2][1] << runnerName[2][2] << runnerName[2][3] << runnerName[2][4] <<endl;
	cout << runnerName[3][0] << runnerName[3][1] << runnerName[3][2] << runnerName[3][3] << runnerName[3][4] << runnerName[3][5] << runnerName[3][6]<< endl;
	cout << runnerName[4][0] << runnerName[4][1] << runnerName[4][2] << runnerName[4][3] << runnerName[4][4] << runnerName[4][5] <<endl;
}

	for (int i=0; i < 7; i++)
{
    cout << milesDay[0][0] << milesDay[0][1]<< milesDay[0][2] << milesDay[0][3] << milesDay[0][4]<< milesDay[0][5] << milesDay[0][6] << milesDay[0][7]<< endl;
	cout << milesDay[1][0] << milesDay[1][1]<< milesDay[1][2] << milesDay[1][3] << milesDay[1][4]<< milesDay[1][5] << milesDay[1][6] << milesDay[1][7]<< endl;
	cout << milesDay[2][0] << milesDay[2][1]<< milesDay[2][2] << milesDay[2][3] << milesDay[2][4]<< milesDay[2][5] << milesDay[2][6] << milesDay[2][7]<< endl;
	cout << milesDay[3][0] << milesDay[3][1]<< milesDay[3][2] << milesDay[3][3] << milesDay[3][4]<< milesDay[3][5] << milesDay[3][6] << milesDay[3][7]<< endl;
	cout << milesDay[4][0] << milesDay[4][1]<< milesDay[4][2] << milesDay[4][3] << milesDay[4][4]<< milesDay[4][5] << milesDay[4][6] << milesDay[4][7]<< endl;
}

	system("pause");    
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#14 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 - 04:13 AM

Ok hm, you are making a loop for something that will run only once. Also theres no reason to use an array if you are gonna make it cout every letter, arrays are made so you can make a more automatic way to get what you want.

For instance if you want to print [0~4][0~8] you can make a loop that starts with 0 and stops at 4 and another one that starts at 0 and stops at 8. So what you will have should look like this:
for(int i=0;i<5;i++)
      {
          for(int j=0;j<9;j++)
          {
             cout<<runnerName[i][j];
          }
          cout<<'\n';
      }



But if you do it like that for names that has less than 9 characters others names will show up, for jason the sentence "jason sam" would show up with that code. So you need to find a way to not let that happen, for now I will not post the how to do that.

Also I recommend you to read more about arrays as you are not using it properly.
Was This Post Helpful? 0
  • +
  • -

#15 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 - 06:38 AM

Why are you printing each individual letter of your runnerName array. You can print the entire name with a single command:

#include <iostream>

using namespace std;

// Define some constants for later use.
const int NumRunners = 5;
const int NumRaces = 7;
const int MaxNameSize = 20;

int main()
{
   // Declare and initialize some runners.
   char runnerName[NumRunners][MaxNameSize]= {"jason","samantha","ravi","sheila","ankit"};
   // Print out the runners names.
   for(int i = 0; i < NumRunners; ++i)
   {
      cout << runnerName[i] << endl;
   }
}




You really should study up on arrays and character sequences.

To print the runner's times after the name you would need to add a second loop, below the cout, to print each element of your milesDay[][] array. This second loop will be inside the first loop.

You are trying to use parallel arrays to printout this information, so the first index milesDay[thisOne][] would use the same index variable you are using to print the runnerName, i in the case of the snippet above.


Jim

This post has been edited by jimblumberg: 08 February 2012 - 06:38 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2