Answer to Question #96948 in C++ for Ankush

Question #96948
Students with register number 1 to register number 50 are assigned to boys labelled as 1,2,3… 50 and students from register number 51 to register number hundred are assigned to girls labelled as 51, 52,…100.Execute various methods to display the set of students such that there is an alternating sequence of register numbers of boys and girls.
1
Expert's answer
2019-10-21T15:32:15-0400
#include <iostream>
#include <ctime>
#include <vector>
using namespace std;
int main()
{
  //first method 
  for(int i =1; i<=50; ++i)
   cout << i << ", "<< 50+i <<", ";
  //second method
  srand(time(NULL));
  vector<int> v;
  while (v.size()<100)
{
  if (v.size()%2==0)
  {
  int g=rand()%50+1;
  int count=0;
  for (int i =0; i<v.size(); ++i)
   if (v[i]==g)
     count++;
   if (count==0)
     v.push_back(g);
  }
  else
  {
  int g=rand()%50+51;
  int count=0;
  for (int i =0; i<v.size(); ++i)
   if (v[i]==g)
     count++;
   if (count==0)
     v.push_back(g);
  }
}
for (int j=0; j<v.size(); ++j)
  cout << v[j] <<",";
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS