how to change the array in for loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SUNIL TYATA
    New Member
    • Feb 2010
    • 18

    how to change the array in for loop

    I ve five different arrary of test scores
    person 1 -> test_score1[4]
    person 2 -> test_score2[4]
    how to change to array of test_score1 to test_score2 in for loop for person1 to person2 to insert the values in test_score array.


    Code:
    for(int i=0;i<5;i++)
    {
    cout<<"Give the name of the student "<<i+1<<": ";
    cin>>names[i];
    cout<<"Give the four test score for "<<names[i];
    
     for(int j=0;j<4;j++)
     {
     cout<<"Test "<<i+1<<" : ";
     cin>>test_score_arr1[i];  //how to change the array of test_score1 to test_score 2 for next loop 
     }
    }
  • BenFedorko
    New Member
    • May 2010
    • 8

    #2
    my idea is to just change your [i] to a [j]. If that's how you have your array set up.

    Code:
    for(int i=0;i<5;i++)
    {
    cout<<"Give the name of the student "<<i+1<<": ";
    cin>>names[i];
    cout<<"Give the four test score for "<<names[i];
     
     for(int j=0;j<4;j++)
     {
     cout<<"Test "<<j+1<<" : "; //this should be a j or else you'll keep reading test 1 for your first student
    cin>>test_score_arr1[j];  // this should also be a j or you'll keep reading values for test_score_arr1[0] for your first student
     }
    }
    but to change your variable you can use a two dimensional array:
    array[number of students][number of tests];

    or change your students to a struct and call
    names[x].test_score_arr 1;
    names[x].test_score_arr 2;
    names[x].test_score_arr 3;
    names[x].test_score_arr 4;
    each time in the function.

    Comment

    • SUNIL TYATA
      New Member
      • Feb 2010
      • 18

      #3
      Originally posted by BenFedorko
      my idea is to just change your [i] to a [j]. If that's how you have your array set up.

      Code:
      for(int i=0;i<5;i++)
      {
      cout<<"Give the name of the student "<<i+1<<": ";
      cin>>names[i];
      cout<<"Give the four test score for "<<names[i];
       
       for(int j=0;j<4;j++)
       {
       cout<<"Test "<<j+1<<" : "; //this should be a j or else you'll keep reading test 1 for your first student
      cin>>test_score_arr1[j];  // this should also be a j or you'll keep reading values for test_score_arr1[0] for your first student
       }
      }
      but to change your variable you can use a two dimensional array:
      array[number of students][number of tests];

      or change your students to a struct and call
      names[x].test_score_arr 1;
      names[x].test_score_arr 2;
      names[x].test_score_arr 3;
      names[x].test_score_arr 4;
      each time in the function.
      I am not allowed to use either two dimensional array or structure in this question. Is there any logical solution?

      Comment

      • AnagJohari
        New Member
        • May 2010
        • 96

        #4
        Originally posted by SUNIL TYATA
        I ve five different arrary of test scores
        person 1 -> test_score1[4]
        person 2 -> test_score2[4]
        how to change to array of test_score1 to test_score2 in for loop for person1 to person2 to insert the values in test_score array.


        Code:
        for(int i=0;i<5;i++)
        {
        cout<<"Give the name of the student "<<i+1<<": ";
        cin>>names[i];
        cout<<"Give the four test score for "<<names[i];
        
         for(int j=0;j<4;j++)
         {
         cout<<"Test "<<i+1<<" : ";
         cin>>test_score_arr1[i];  //how to change the array of test_score1 to test_score 2 for next loop 
         }
        }
        u take the third temp array ok & transfer the value of an array1 to temp array
        by using loop one by one
        after that take t array 1 & transfer that value from array 2 using loop
        after that take array 2 & transfer the value from temp.
        i give u some sort of logic by this way it will done
        i think

        Comment

        • BenFedorko
          New Member
          • May 2010
          • 8

          #5
          Originally posted by SUNIL TYATA
          I ve five different arrary of test scores
          person 1 -> test_score1[4]
          person 2 -> test_score2[4]
          how to change to array of test_score1 to test_score2 in for loop for person1 to person2 to insert the values in test_score array.


          Code:
          for(int i=0;i<5;i++)
          {
          cout<<"Give the name of the student "<<i+1<<": ";
          cin>>names[i];
          cout<<"Give the four test score for "<<names[i];
          
           for(int j=0;j<4;j++)
           {
           cout<<"Test "<<i+1<<" : ";
           cin>>test_score_arr1[i];  //how to change the array of test_score1 to test_score 2 for next loop 
           }
          }
          if you're writing to a file you could just "fout" that information at the end of the function and loop the whole thing.

          Comment

          Working...