C++ programming // Please check make correction..there have so many errors. plea
ID: 3824272 • Letter: C
Question
C++ programming
// Please check make correction..there have so many errors. please make improvement from this code!
#include <iostream>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <conio.h>
#include <algorithm>
using namespace std;
//The functions to perform linked list insertion
//linked list lNode
struct node
{
int number;
struct node* link;
};
struct node* SortedMerge(struct node* a, struct node* b);
void FrontBackSplit(struct node* source, struct node** frontRef, struct node** backRef);
void MergeSort(struct node** headRef) //merge sort for linked list
{
struct node* head = *headRef;
struct node* a;
struct node* b;
/* Base case -- length 0 or 1 */
if ((head == NULL) || (head->link == NULL))
{
return;
}
/* Split head into 'a' and 'b' sublists */
FrontBackSplit(head, &a, &b);
/* Recursively sort the sublists */
MergeSort(&a);
MergeSort(&b);
/* answer = merge the two sorted lists together */
*headRef = SortedMerge(a, b);
}
void FrontBackSplit(struct node* source, struct node** frontRef, struct node** backRef)
{
struct node* fast;
struct node* slow;
if (source==NULL || source->link==NULL)
{
/* length < 2 cases */
*frontRef = source;
*backRef = NULL;
}
else
{
slow = source;
fast = source->link;
/* Advance 'fast' two nodes, and advance 'slow' one node */
while (fast != NULL)
{
fast = fast->link;
if (fast != NULL)
{
slow = slow->link;
fast = fast->link;
}
}
/* 'slow' is before the midpoint in the list, so split it in two
at that point. */
*frontRef = source;
*backRef = slow->link;
slow->link = NULL;
}
}
struct node* SortedMerge(struct node* a, struct node* b)
{
struct node* result = NULL;
/* Base cases */
if (a == NULL)
return(b);
else if (b==NULL)
return(a);
/* Pick either a or b, and recur */
if (a->number <= b->number)
{
result = a;
result->link = SortedMerge(a->link, b);
}
else
{
result = b;
result->link = SortedMerge(a, b->link);
}
return(result);
}
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->number = new_data;
/* link the old list off the new node */
new_node->link = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/*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 node *newNode(int newRandom)
{
struct node* nodeNew = (struct node*) malloc(sizeof(struct node));
nodeNew->number = newRandom;
nodeNew->link = NULL;
return nodeNew;
}
void printNode(struct node *curNode)
{
struct node *vtem = curNode;
int x = 0;
while(x != 9900)
{
vtem = vtem->link;
x++;
}
int line = 0;
while (vtem != NULL && x!= 10100)
{
if (line % 20 == 0)
cout << endl;
line++;
printf("%d ", vtem->number);
vtem = vtem->link;
x++;
}
}
//The main
int main()
{
cout << "ARRAY" << endl;
clock_t start, end;
start = clock();
int Numb, vi, vj, vk, numArray[20000], count;
int max = 10000;
int min = -10000;
srand(time(NULL));
//change the number to 2000000
/*for (vi = 0; vi<2000000; vi++)
{
Numb = rand()%((max - min) + 1) + min;
//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;
}*/
for(vi =0; vi<20000; vi++)
{
Numb = rand()%((max - min) + 1) + min;
numArray[vi] = Numb;
}
sort(numArray,numArray+20000);
int line = 0;
for (vi = 9900; vi<10100; 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 <int> numvect(20000);
/*for (vi = 0; vi<2000000; vi++)
{
Numb = rand()%((max - min) + 1) + min;
//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;
}*/
for(vi =0; vi<20000; vi++)
{
Numb = rand()%((max - min) + 1) + min;
numvect[vi] = Numb;
}
sort(numvect.begin(),numvect.end());
line = 0;
for (vi = 9900; vi<10100; 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 node* head = NULL;
//Numb = rand()%((max - min) + 1) + min;
//Numb = rand() % 20000 - 10000;
//struct node *nodeNew = newNode(Numb);
for (vi = 0; vi<20000; vi++)
{
Numb = rand()%((max - min) + 1) + min;
push(&head, Numb);
//Numb = rand() % 20000 - 10000;
//nodeNew = newNode(Numb);
}
MergeSort(&head);
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 <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
int iSecret, iGuess;
srand ( time(NULL) );
iSecret = rand() % 10 + 1;
do {
printf ("Guess the number (1 to 10): ");
scanf ("%d",&iGuess);
if (iSecret<iGuess) puts ("The secret number is lower");
else if (iSecret>iGuess) puts ("The secret number is higher");
} while (iSecret!=iGuess);
puts ("Congratulations!");
return 0;
}
#include <iostream>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <conio.h>
#include <algorithm>
{
int number;
struct node* link;
};
struct node* SortedMerge(struct node* a, struct node* b);
void FrontBackSplit(struct node* source, struct node** frontRef, struct node** backRef);
void MergeSort(struct node** headRef) //merge sort for linked list
{
struct node* head = *headRef;
struct node* a;
struct node* b;
if ((head == NULL) || (head->link == NULL))
{
return;
}
Split head into 'a' and 'b' sublists
FrontBackSplit(head, &a, &b);
MergeSort(&a);
MergeSort(&b);
answer = merge the two sorted lists together
*headRef = SortedMerge(a, b);
}
void FrontBackSplit(struct node* source, struct node** frontRef, struct node** backRef)
{
struct node* fast;
struct node* slow;
if (source==NULL || source->link==NULL)
{
length < 2 cases
*frontRef = source;
*backRef = NULL;
}
else
{
slow = source;
fast = source->link;
while (fast != NULL)
{
fast = fast->link;
if (fast != NULL)
{
slow = slow->link;
fast = fast->link;
}
}
at that point. */
*frontRef = source;
*backRef = slow->link;
slow->link = NULL;
}
}
struct node* SortedMerge(struct node* a, struct node* b)
{
struct node* result = NULL;
/* Base cases */
if (a == NULL)
return(b);
else if (b==NULL)
return(a);
/* Pick either a or b, and recur */
if (a->number <= b->number)
{
result = a;
result->link = SortedMerge(a->link, b);
}
else
{
result = b;
result->link = SortedMerge(a, b->link);
}
return(result);
}
void push(struct node** head_ref, int new_data)
{
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->number = new_data;
/* link the old list off the new node */
new_node->link = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/*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 node *newNode(int newRandom)
{
struct node* nodeNew = (struct node*) malloc(sizeof(struct node));
nodeNew->number = newRandom;
nodeNew->link = NULL;
return nodeNew;
}
void printNode(struct node *curNode)
{
struct node *vtem = curNode;
int x = 0;
while(x != 9900)
{
vtem = vtem->link;
x++;
}
int line = 0;
while (vtem != NULL && x!= 10100)
{
if (line % 20 == 0)
cout << endl;
line++;
printf("%d ", vtem->number);
vtem = vtem->link;
x++;
}
}
//The main
int main()
{
cout << "ARRAY" << endl;
clock_t start, end;
start = clock();
int Numb, vi, vj, vk, numArray[20000], count;
int max = 10000;
int min = -10000;
srand(time(NULL));
//change the number to 2000000
/*for (vi = 0; vi<2000000; vi++)
{
Numb = rand()%((max - min) + 1) + min;
//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;
}*/
for(vi =0; vi<20000; vi++)
{
Numb = rand()%((max - min) + 1) + min;
numArray[vi] = Numb;
}
sort(numArray,numArray+20000);
int line = 0;
for (vi = 9900; vi<10100; 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 <int> numvect(20000);
/*for (vi = 0; vi<2000000; vi++)
{
Numb = rand()%((max - min) + 1) + min;
//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;
}*/
for(vi =0; vi<20000; vi++)
{
Numb = rand()%((max - min) + 1) + min;
numvect[vi] = Numb;
}
sort(numvect.begin(),numvect.end());
line = 0;
for (vi = 9900; vi<10100; 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 node* head = NULL;
//Numb = rand()%((max - min) + 1) + min;
//Numb = rand() % 20000 - 10000;
//struct node *nodeNew = newNode(Numb);
for (vi = 0; vi<20000; vi++)
{
Numb = rand()%((max - min) + 1) + min;
push(&head, Numb);
//Numb = rand() % 20000 - 10000;
//nodeNew = newNode(Numb);
}
MergeSort(&head);
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;
}
im giving some example starting
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.