Jump to content

C++ Help

Go to solution Solved by OuchMyFaceyHole,
void createRandomNumbers(int list[]){srand(time(0}};  for (int r = 0; r < SIZE ; r++){list[r] = (rand() % 200) + 1; } selectionSort(list, SIZE); for (int r=0; r<SIZE;r++){     cout << list[r] << endl; } }

Le fixed

Hi everyone,

 

I have a code that its working fine, but its giving me negative numbers on my array. on this array I should not have anything thats less than 0. 

 

I don't really know what to do right now.

 

thank you for any help.

 

here is the code.

 

 #include <iostream>#include <cmath>#include <fstream>#include <string>#include <cstdlib>#include <ctime> using namespace std;int value;int positionLinear;int positionBinary;char answer;  //Constant Numbersconst int SIZE = 200;  //Fuction Prototypesint linearSearch(const int[], int, int);int binarySearch(const int[], int, int);void selectionSort(int [], int);void checkAnswer();void createRandomNumbers(int [], int);           // Main Fuctionint main(){const int min_Rvalue = 0;const int max_Rvalue = 200;int list[200];createRandomNumbers(list, SIZE);cout << "Please enter a random key from keyboard in between 1 and 200 " << endl;cin >> value;int linearSearch(const int list[] , int SIZE, int value);// (random Array, SIZE, value)positionLinear = linearSearch(list, SIZE, value); // Result Transfer from Linear Search int binarySearch(const int list[], int SIZE, int value ); // (random Array, SIZE, value)positionBinary = binarySearch(list, SIZE, value); // Result Transfer from Binary Search   // Binary Searchif (positionBinary == -1){cout << "The number couldnt be found on the Binary Search" << endl;}else{cout << "On the Binary Search the number is found at position " << positionBinary << endl;} // Linear Searchif (positionLinear == -1)cout << "Could find the number on the Linear Search" << endl;else{// Position has been found on the Linear Search.cout << "On the Linear Search the number is found at position " << positionLinear << endl;} checkAnswer();  system("pause"); return 0;}    int linearSearch(const int list[], int numElems, int value){ int index = 0;int positionLinear = -1;bool found = false; while (index < numElems && !found){ if (list[index] == value){found = true;positionLinear = index;}index++;}return positionLinear;} int binarySearch(const int list[], int numElems, int value){int first = 0,last = numElems - 1,middle,positionBinary = -1;bool found = false; while (!found && first <= last){middle = (first + last) / 2;if (list[middle] == value){found = true;positionBinary = middle;}else if (list[middle] > value){last = middle - 1;}elsefirst = middle + 1; }return positionBinary;} void checkAnswer(){cout << "Would you like to try a new set of numbers? (Y/N)";cin >> answer; if (answer == 'Y' || answer == 'y'){int main();}else if (answer == 'N' || answer == 'n'){exit(0);}elsecout << "You have entered a invalid choice." << endl;cout << "Please answer with a Y or N" << endl;checkAnswer();  } void createRandomNumbers(int list[], int SIZE){unsigned seed;seed = time(0);srand(seed);  for (int r = 0; r < SIZE ; r++){list[r] = (rand() % 200) + 1; selectionSort(list, SIZE);cout << list[r] << endl;}   } void selectionSort(int list[], int SIZE){ bool swap;int temp; do{swap = false;for (int count = 0; count < (SIZE - 1); count++){if (list[count] > list[count + 1]){temp = list[count];list[count] = list[count + 1];list[count + 1] = temp;swap = true;}}} while (swap);} 
Link to comment
https://linustechtips.com/topic/502087-c-help/
Share on other sites

Link to post
Share on other sites

Look up unsigned integers, this basically just tells c++ the number can't be negative. I think that's what you want

I actually have that, but for some reason still showing negative numbers. my problem mainly is when I sort it out. If i remove the part to sort everything work, but my search for binary doesnt. My guess is that something on the sorting section I have the problem. I cant solve, and my teacher has no clue what to do.

Link to comment
https://linustechtips.com/topic/502087-c-help/#findComment-6703525
Share on other sites

Link to post
Share on other sites

I actually have that, but for some reason still showing negative numbers. my problem mainly is when I sort it out. If i remove the part to sort everything work, but my search for binary doesnt. My guess is that something on the sorting section I have the problem. I cant solve, and my teacher has no clue what to do.

I've compiled it and I don't know what this is supposed to do. I get a large list of negative numbers, 0s, then numbers. Is it supposed to pick the nearest number in that list to what I type? It says 87 is found at position 104 but 87 isn't in that list nor is 104.

 

Also I would change the "Please enter a random key from the keyboard in between 1 and 200" to "Enter a number between 1 and 200", its just more clear

Link to comment
https://linustechtips.com/topic/502087-c-help/#findComment-6703657
Share on other sites

Link to post
Share on other sites

I've compiled it and I don't know what this is supposed to do. I get a large list of negative numbers, 0s, then numbers. Is it supposed to pick the nearest number in that list to what I type? It says 87 is found at position 104 but 87 isn't in that list nor is 104.

 

Also I would change the "Please enter a random key from the keyboard in between 1 and 200" to "Enter a number between 1 and 200", its just more clear

the program needs to have a set of random numbers from 1 to 200(they can repeat), so after the list shows, you enter a number that you think that may be on the list. The search will start after you enter the number, it will do a linear search and a binary search, giving the position its located on that list for each search.

 

I get the negative numbers also, I want to remove them. I cant find the solution for that ://

Link to comment
https://linustechtips.com/topic/502087-c-help/#findComment-6703736
Share on other sites

Link to post
Share on other sites

So the negative numbers are in your random number array?

Main Gaming PC - i9 10850k @ 5GHz - EVGA XC Ultra 2080ti with Heatkiller 4 - Asrock Z490 Taichi - Corsair H115i - 32GB GSkill Ripjaws V 3600 CL16 OC'd to 3733 - HX850i - Samsung NVME 256GB SSD - Samsung 3.2TB PCIe 8x Enterprise NVMe - Toshiba 3TB 7200RPM HD - Lian Li Air

 

Proxmox Server - i7 8700k @ 4.5Ghz - 32GB EVGA 3000 CL15 OC'd to 3200 - Asus Strix Z370-E Gaming - Oracle F80 800GB Enterprise SSD, LSI SAS running 3 4TB and 2 6TB (Both Raid Z0), Samsung 840Pro 120GB - Phanteks Enthoo Pro

 

Super Server - i9 7980Xe @ 4.5GHz - 64GB 3200MHz Cl16 - Asrock X299 Professional - Nvidia Telsa K20 -Sandisk 512GB Enterprise SATA SSD, 128GB Seagate SATA SSD, 1.5TB WD Green (Over 9 years of power on time) - Phanteks Enthoo Pro 2

 

Laptop - 2019 Macbook Pro 16" - i7 - 16GB - 512GB - 5500M 8GB - Thermal Pads and Graphite Tape modded

 

Smart Phones - iPhone X - 64GB, AT&T, iOS 13.3 iPhone 6 : 16gb, AT&T, iOS 12 iPhone 4 : 16gb, AT&T Go Phone, iOS 7.1.1 Jailbroken. iPhone 3G : 8gb, AT&T Go Phone, iOS 4.2.1 Jailbroken.

 

Link to comment
https://linustechtips.com/topic/502087-c-help/#findComment-6705096
Share on other sites

Link to post
Share on other sites

I'm not a C++ guy, so I could be wrong, but I think the issue (or at least one issue) is that you're trying to sort the array when it has garbage values.

 

For example if I run this code.

int list[10];for (int i = 0; i < 10; i++){    cout << list[i] << endl;}

This is my output

6422240642229664224761964954096-1536058162-21964929898196493014142010726422368

You're not initializing the values of list here.

int list[200];createRandomNumbers(list, SIZE);

Nor are you completely filling the array with random values before starting the sort here.

for (int r = 0; r < SIZE ; r++){    list[r] = (rand() % 200) + 1;     selectionSort(list, SIZE);    cout << list[r] << endl;}

So you're working with the garbage values. I think all you need to do is move the call to selectionSort outside the loop.

void createRandomNumbers(int list[], int SIZE) {    unsigned seed;    seed = time(0);    srand(seed);    for (int r = 0; r < SIZE; r++) {        list[r] = (rand() % 200) + 1;    }    selectionSort(list, SIZE);}
Link to comment
https://linustechtips.com/topic/502087-c-help/#findComment-6707479
Share on other sites

Link to post
Share on other sites

 

I'm not a C++ guy, so I could be wrong, but I think the issue (or at least one issue) is that you're trying to sort the array when it has invalid values.

 

For example if I run this code.

int list[10];for (int i = 0; i < 10; i++){    cout << list[i] << endl;}

This is my output

6422240642229664224761964954096-1536058162-21964929898196493014142010726422368

You're not initializing the value of list here.

int list[200];createRandomNumbers(list, SIZE);

Nor are you completely filling the array with random values before starting the sort here.

for (int r = 0; r < SIZE ; r++){    list[r] = (rand() % 200) + 1;     selectionSort(list, SIZE);    cout << list[r] << endl;}

So you're working with the garbage values. I think all you need to do is move the call to selectionSort outside the loop.

void createRandomNumbers(int list[], int SIZE) {    unsigned seed;    seed = time(0);    srand(seed);    for (int r = 0; r < SIZE; r++) {        list[r] = (rand() % 200) + 1;    }    selectionSort(list, SIZE);}

if that works, which I hope it does. I need to place a cout for that array, to show me the numbers that are on the array.

 

also what should I do to initialize the value on the second part.

Link to comment
https://linustechtips.com/topic/502087-c-help/#findComment-6707549
Share on other sites

Link to post
Share on other sites

Correct

@madnight3 is right. Can't believe I looked over that. Also you don't need to pass size as it is a global constant and there is no need for an extra variable for time as you can just run srand(time(0)) just fine.

Main Gaming PC - i9 10850k @ 5GHz - EVGA XC Ultra 2080ti with Heatkiller 4 - Asrock Z490 Taichi - Corsair H115i - 32GB GSkill Ripjaws V 3600 CL16 OC'd to 3733 - HX850i - Samsung NVME 256GB SSD - Samsung 3.2TB PCIe 8x Enterprise NVMe - Toshiba 3TB 7200RPM HD - Lian Li Air

 

Proxmox Server - i7 8700k @ 4.5Ghz - 32GB EVGA 3000 CL15 OC'd to 3200 - Asus Strix Z370-E Gaming - Oracle F80 800GB Enterprise SSD, LSI SAS running 3 4TB and 2 6TB (Both Raid Z0), Samsung 840Pro 120GB - Phanteks Enthoo Pro

 

Super Server - i9 7980Xe @ 4.5GHz - 64GB 3200MHz Cl16 - Asrock X299 Professional - Nvidia Telsa K20 -Sandisk 512GB Enterprise SATA SSD, 128GB Seagate SATA SSD, 1.5TB WD Green (Over 9 years of power on time) - Phanteks Enthoo Pro 2

 

Laptop - 2019 Macbook Pro 16" - i7 - 16GB - 512GB - 5500M 8GB - Thermal Pads and Graphite Tape modded

 

Smart Phones - iPhone X - 64GB, AT&T, iOS 13.3 iPhone 6 : 16gb, AT&T, iOS 12 iPhone 4 : 16gb, AT&T Go Phone, iOS 7.1.1 Jailbroken. iPhone 3G : 8gb, AT&T Go Phone, iOS 4.2.1 Jailbroken.

 

Link to comment
https://linustechtips.com/topic/502087-c-help/#findComment-6707570
Share on other sites

Link to post
Share on other sites

@madnight3 is right. Can't believe I looked over that. Also you don't need to pass size as it is a global constant and there is no need for an extra variable for time as you can just run srand(time(0)) just fine.

I fixed the random which is great, but I need to get the selection sort showing from the array without the garbage number.

 

I try the program with showing the numbers in order, and it work it. 

 

but unfortunately i need to show in order =/.

Link to comment
https://linustechtips.com/topic/502087-c-help/#findComment-6707651
Share on other sites

Link to post
Share on other sites

void createRandomNumbers(int list[]){srand(time(0}};  for (int r = 0; r < SIZE ; r++){list[r] = (rand() % 200) + 1; } selectionSort(list, SIZE); for (int r=0; r<SIZE;r++){     cout << list[r] << endl; } }

Le fixed

Main Gaming PC - i9 10850k @ 5GHz - EVGA XC Ultra 2080ti with Heatkiller 4 - Asrock Z490 Taichi - Corsair H115i - 32GB GSkill Ripjaws V 3600 CL16 OC'd to 3733 - HX850i - Samsung NVME 256GB SSD - Samsung 3.2TB PCIe 8x Enterprise NVMe - Toshiba 3TB 7200RPM HD - Lian Li Air

 

Proxmox Server - i7 8700k @ 4.5Ghz - 32GB EVGA 3000 CL15 OC'd to 3200 - Asus Strix Z370-E Gaming - Oracle F80 800GB Enterprise SSD, LSI SAS running 3 4TB and 2 6TB (Both Raid Z0), Samsung 840Pro 120GB - Phanteks Enthoo Pro

 

Super Server - i9 7980Xe @ 4.5GHz - 64GB 3200MHz Cl16 - Asrock X299 Professional - Nvidia Telsa K20 -Sandisk 512GB Enterprise SATA SSD, 128GB Seagate SATA SSD, 1.5TB WD Green (Over 9 years of power on time) - Phanteks Enthoo Pro 2

 

Laptop - 2019 Macbook Pro 16" - i7 - 16GB - 512GB - 5500M 8GB - Thermal Pads and Graphite Tape modded

 

Smart Phones - iPhone X - 64GB, AT&T, iOS 13.3 iPhone 6 : 16gb, AT&T, iOS 12 iPhone 4 : 16gb, AT&T Go Phone, iOS 7.1.1 Jailbroken. iPhone 3G : 8gb, AT&T Go Phone, iOS 4.2.1 Jailbroken.

 

Link to comment
https://linustechtips.com/topic/502087-c-help/#findComment-6707661
Share on other sites

Link to post
Share on other sites

void createRandomNumbers(int list[]){srand(time(0}};  for (int r = 0; r < SIZE ; r++){list[r] = (rand() % 200) + 1; } selectionSort(list, SIZE); for (int r=0; r<SIZE;r++){     cout << list[r] << endl; } }

Le fixed

 

Thaaaaannnnnkkk yooooou!!!!!!!

 

Now its working =DDDDD

 

Really appreciate the help =D

 

:D  :D  :D  :D  :D  :D  :D  :D

 

 

 

Also want to thank everyone that help it.

@madkinght3

Link to comment
https://linustechtips.com/topic/502087-c-help/#findComment-6707705
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×