开发者

Turn 1 C++ Loop into 2

开发者 https://www.devze.com 2023-01-29 13:31 出处:网络
I need a little programming help for class. I wrote the code below, but my teacher says I need to utilize 2 for loops when reading the data fi开发者_JS百科le. I\'m not sure how to do that... any sugge

I need a little programming help for class. I wrote the code below, but my teacher says I need to utilize 2 for loops when reading the data fi开发者_JS百科le. I'm not sure how to do that... any suggestions?

My Code:

ifstream infile;
infile.open("dive.txt");
for(int i = 0; i <= 6; i++)
    infile >> contestantNames[i][0] >> contestantNames[i][1] >> judgeScores[i][0] >> judgeScores[i][1] >> judgeScores[i][2] >> judgeScores[i][3] >> judgeScores[i][4] >> judgeScores[i][5] >> judgeScores[i][6] >> judgeScores[i][7];
infile.close();


Instead of repetitiously writing

infile >> contestantNames[0][0] >> contestantNames[0][1] >> judgeScores[0][0] >> judgeScores[0][1] >> judgeScores[0][2] >> judgeScores[0][3] >> judgeScores[0][4] >> judgeScores[0][5] >> judgeScores[0][6] >> judgeScores[0][7];
infile >> contestantNames[1][0] >> contestantNames[1][1] >> judgeScores[1][0] >> judgeScores[1][1] >> judgeScores[1][2] >> judgeScores[1][3] >> judgeScores[1][4] >> judgeScores[1][5] >> judgeScores[1][6] >> judgeScores[1][7];
infile >> contestantNames[2][0] >> contestantNames[2][1] >> judgeScores[2][0] >> judgeScores[2][1] >> judgeScores[2][2] >> judgeScores[2][3] >> judgeScores[2][4] >> judgeScores[2][5] >> judgeScores[2][6] >> judgeScores[2][7];
infile >> contestantNames[3][0] >> contestantNames[3][1] >> judgeScores[3][0] >> judgeScores[3][1] >> judgeScores[3][2] >> judgeScores[3][3] >> judgeScores[3][4] >> judgeScores[3][5] >> judgeScores[3][6] >> judgeScores[3][7];
infile >> contestantNames[4][0] >> contestantNames[4][1] >> judgeScores[4][0] >> judgeScores[4][1] >> judgeScores[4][2] >> judgeScores[4][3] >> judgeScores[4][4] >> judgeScores[4][5] >> judgeScores[4][6] >> judgeScores[4][7];
infile >> contestantNames[5][0] >> contestantNames[5][1] >> judgeScores[5][0] >> judgeScores[5][1] >> judgeScores[5][2] >> judgeScores[5][3] >> judgeScores[5][4] >> judgeScores[5][5] >> judgeScores[5][6] >> judgeScores[5][7];
infile >> contestantNames[6][0] >> contestantNames[6][1] >> judgeScores[6][0] >> judgeScores[6][1] >> judgeScores[6][2] >> judgeScores[6][3] >> judgeScores[6][4] >> judgeScores[6][5] >> judgeScores[6][6] >> judgeScores[6][7];

you wrote a for loop.

Where else is there repetition?


Your two for loops would be

for (int i = 0; i <= 6; i++) {
    infile >> contestantNames[i][0] >> contestantNames[i][1];
    for (int j = 0; j <= 7; j++) {
        infile >> judgeScores[i][j];
    }
}

Your instructor want you to simplify that continuous line of judgeScores into a for loop which is much simpler and easier to read.


ifstream infile;
infile.open("dive.txt");

for (int i = 0; i <= 6; i++)
{
  infile >> contestantNames[i][0] >> contestantNames[i][1];

  // The next part is what your teacher was talking about.
  // You wrote judgeScores[i][0] >> judgeScores[i][1] >> ...
  // seven times, which is pretty redundant. Programmers are *extremely* lazy
  // so we loop constantly, wherever possible:

  for (int j = 0; j <= 7; j++)
  {
     infile >> judgeScores[i][j];
  }
}

infile.close();


You teacher probably wants you to consider the case when there are more than two contenstants. Something along the lines of

nc = 6;  // the number of contenstans
for (int c=0; c<nc; c++) {
  // other for loop here...
}


I think your instructor means to loop once over all the contestants, and loop again to input each contestant's information (name and score.)

So try something like this:

ifstream infile;
infile.open("dive.txt");
for(int i = 0; i <= 6; i++) {
    infile >> contestantNames[i][0] >> contestantNames[i][1];
    for (int j = 0; i <= 7; j++) {
         infile >> judgeScores[i][j];
    }
}
infile.close();

Note: the brackets aren't required, since each statement consists of one line. This is the exception to the rule, however - if there is more than 1 statement in the for loop, brackets would be required. (As you may know.)


Surprise you teacher with even three loops:

ifstream infile;
infile.open("dive.txt");
for(int i = 0; i <= 6; i++)
{
    for (int j = 0; j < 2; ++j)
    {
        infile >> contestantNames[i][j];
    }

    for (int j = 0; j < 8; ++j)
    {
       infile >> judgeScores[i][j];
    }
}
infile.close();


Put a while loop checking for the end of file (EOF) in case there are more than 6 lines. Test cases for bad data wouldn't hurt either.

0

精彩评论

暂无评论...
验证码 换一张
取 消