Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Time the creation of an array that holds two million integer random values b

ID: 3816059 • Letter: C

Question

C++

Time the creation of an array that holds two million integer random values between -10,000 and 10,000, sorted.

   Print out the middle 200 values, 20 values per line.

Time the creation of a vector that holds

integer random values between -10,000 and 10,000, sorted.

Print out the middle 200 values, 20 values per line.

Finally, time the creation of a linked list that holds two million integer random values between -10,000 and 10,000, sorted.

Print out the middle 200 values, 20 values per line.

Print out the times it took to create each type of thing, labeled appropriately.

** The values in each should be entered into the array / vector / linked list sorted .

I realize there are other ways to do this, but for this problem it needs to be done this way.

Building an array with the following input numbers: 3, 2, 9, 5, 1

3    

2 3    

2 3 9   

2 3 5 9  

1 2 3 5 9
_______________________________________________________________________

//could you please check my code and change it whatever you want for improvement !

#include <iostream>

#include <ctime>

#include <vector>

using namespace std;

//The functions to perform linked list insertion

//linked list lNode
struct lNode
{
int number;
struct lNode* link;
};

void sortedInsert(struct lNode** pointhead, struct lNode* nodeNew)
{
struct lNode* head;
if (*pointhead == NULL || (*pointhead)->number >= nodeNew->number)
{
  nodeNew->link = *pointhead;
  *pointhead = nodeNew;
}
else
{

  head = *pointhead;
  while (head->link != NULL &&
   head->link->number < nodeNew->number)
  {
   head = head->link;
  }
  nodeNew->link = head->link;
  head->link = nodeNew;
}
}


struct lNode *newNode(int newRandom)
{
struct lNode* nodeNew =
  (struct lNode*) malloc(sizeof(struct lNode));

nodeNew->number = newRandom;
nodeNew->link = NULL;

return nodeNew;
}


void printNode(struct lNode *curNode)
{
struct lNode *vtem = curNode;
while (vtem != NULL)
{
  printf("%d ", vtem->number);
  vtem = vtem->link;
}
}

//The main

int main()
{

cout << "ARRAY" << endl;
clock_t start, end;
start = clock();
int Numb, vi, vj, vk, numArray[20000], count;

srand(time(NULL));

//change the number to 2000000
for (vi = 0; vi<20000; vi++)
{
  Numb = rand() % 20000 - 10000;
  vj = 0;
  while (numArray[vj]   {
   vj = vj + 1;
  }

  for (vk = vi; vk>vj; vk--)
  {
   numArray[vk + 1] = numArray[vk];
  }
  numArray[vj] = Numb;
}
int line = 0;
for (vi = 9900; vi<11100; vi++)
{
  if (line % 20 == 0)
   cout << endl;

  cout << numArray[vi];
  line++;
}

end = clock();
double cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
cout << " For array the time taken = " << cpu_time_used << endl;

cout << "Vector";
start = clock();
vector numvect(20000);

for (vi = 0; vi<20000; vi++)
{
  Numb = rand() % 20000 - 10000;
  vj = 0;
  while (numvect[vj]   {
   vj = vj + 1;
  }

  for (vk = vi - 1; vk>vj; vk--)
  {
   numvect[vk + 1] = numvect[vk];
  }
  numvect[vj] = Numb;
}
line = 0;
for (vi = 9900; vi<11100; vi++)
{
  if (line % 20 == 0)
   cout << endl;

  cout << numvect[vi];
  line++;
}

end = clock();
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
cout << " For vector the time taken = " << cpu_time_used << endl;


cout << " Linked list" << endl;
start = clock();
struct lNode* head = NULL;
Numb = rand() % 20000 - 10000;
struct lNode *nodeNew = newNode(Numb);

for (vi = 0; vi<20000; vi++)
{
  sortedInsert(&head, nodeNew);
  Numb = rand() % 20000 - 10000;
  nodeNew = newNode(Numb);
}

printNode(head);
end = clock();
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
cout << " For Linked list the time taken = " << cpu_time_used << endl;

system("pause");
return 0;
}
#include   
#include
#include

using namespace std;

//The functions to perform linked list insertion

//linked list lNode
struct lNode
{
int number;
struct lNode* link;
};

void sortedInsert(struct lNode** pointhead, struct lNode* nodeNew)
{
struct lNode* head;
if (*pointhead == NULL || (*pointhead)->number >= nodeNew->number)
{
  nodeNew->link = *pointhead;
  *pointhead = nodeNew;
}
else
{

  head = *pointhead;
  while (head->link != NULL &&
   head->link->number < nodeNew->number)
  {
   head = head->link;
  }
  nodeNew->link = head->link;
  head->link = nodeNew;
}
}


struct lNode *newNode(int newRandom)
{
struct lNode* nodeNew =
  (struct lNode*) malloc(sizeof(struct lNode));

nodeNew->number = newRandom;
nodeNew->link = NULL;

return nodeNew;
}


void printNode(struct lNode *curNode)
{
struct lNode *vtem = curNode;
while (vtem != NULL)
{
  printf("%d ", vtem->number);
  vtem = vtem->link;
}
}

//The main

int main()
{

cout << "ARRAY" << endl;
clock_t start, end;
start = clock();
int Numb, vi, vj, vk, numArray[20000], count;

srand(time(NULL));

//change the number to 2000000
for (vi = 0; vi<20000; vi++)
{
  Numb = rand() % 20000 - 10000;
  vj = 0;
  while (numArray[vj]   {
   vj = vj + 1;
  }

  for (vk = vi; vk>vj; vk--)
  {
   numArray[vk + 1] = numArray[vk];
  }
  numArray[vj] = Numb;
}
int line = 0;
for (vi = 9900; vi<11100; vi++)
{
  if (line % 20 == 0)
   cout << endl;

  cout << numArray[vi];
  line++;
}

end = clock();
double cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
cout << " For array the time taken = " << cpu_time_used << endl;

cout << "Vector";
start = clock();
vector numvect(20000);

for (vi = 0; vi<20000; vi++)
{
  Numb = rand() % 20000 - 10000;
  vj = 0;
  while (numvect[vj]   {
   vj = vj + 1;
  }

  for (vk = vi - 1; vk>vj; vk--)
  {
   numvect[vk + 1] = numvect[vk];
  }
  numvect[vj] = Numb;
}
line = 0;
for (vi = 9900; vi<11100; vi++)
{
  if (line % 20 == 0)
   cout << endl;

  cout << numvect[vi];
  line++;
}

end = clock();
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
cout << " For vector the time taken = " << cpu_time_used << endl;


cout << " Linked list" << endl;
start = clock();
struct lNode* head = NULL;
Numb = rand() % 20000 - 10000;
struct lNode *nodeNew = newNode(Numb);

for (vi = 0; vi<20000; vi++)
{
  sortedInsert(&head, nodeNew);
  Numb = rand() % 20000 - 10000;
  nodeNew = newNode(Numb);
}

printNode(head);
end = clock();
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
cout << " For Linked list the time taken = " << cpu_time_used << endl;

system("pause");
return 0;
}

Explanation / Answer

#include <iostream>
using namespace std;
int main()
{
srand (time(NULL));
int array[10000000];
for(int i = 0; i < 10000000; i++){
array[i] = (rand() % 10000000) + 1;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote