I am trying to implement a radix sort using queue structure but it's giving me an error. can anyone help me with this please. thanks
using the c++ library queue, the program works perfectly fine so there is something wrong with my queue structure or the way I initialize the array of 10 queues or maybe the way I use pointers.btw I am a beginner. thanks
#include<iostream>
#include<cstdlib>
#include<ctime>
//#include<queue>
#include<cmath>
using namespace std;
struct node{
int value;
node* next;
};
struct que{
node* frontt;
node* rear;
};
void enqueue(que* &queu,int value)
{
node* newNode = new node;
newNode->value = value;
newNode->next = nullptr;
if(queu->rear !=nullptr)
{
queu->rear->next = newNode;
queu->rear = newNode;
} else{
queu->frontt = queu->rear = newNode;
}
}
int dequeue(que* &queu)
{
node* temp = new node;
int returnValue;
if(queu->frontt){
temp = queu->frontt;
returnValue = temp->value;
queu->frontt = queu->frontt->next;
delete temp;
if(!queu->frontt){
queu->rear = nullptr;
}
}
return returnValue;
}
void radixSort(int q[], int n);
int main() {
const int MAX = 9;
int radix[MAX] = {2, 543, 23, 123, 900, 4, 3, 2, 223};
for (int j = 0; j < MAX; j++)
cout << radix[j] << " ";
cout << endl << endl;
radixSort(radix, MAX);
for (int j = 0; j < MAX; j++)
cout << radix[j] << " ";
cout << endl << endl;
return 0;
}
void radixSort(int arr[], int n) {
que* arrayOfQues[10];
//queue<int> arrayOfQues[10]; //one array per possible digit
int maxDigits = 3; //holds amount of digits in largest number
int currentDigit = 0; //starting base for decimal digit
while (currentDigit < maxDigits) {
for(int i = 0; i < n; i++){ //loop through whole array
int divisor = pow(10, currentDigit);
int num = arr[i]; //set to current value of array position
int digitValue = static_cast<int>((num / divisor) % 10); //get the decimal digit at current digit
enqueue(arrayOfQues[digitValue],num);
//arrayOfQues[digitValue].push(num); //put digits in corresponding arrayOfQues
}
int i = 0;
for(int k = 0; k < 10; k++){ //loop through all arrayOfQues
while(arrayOfQues[k]->frontt != nullptr){
//while(!arrayOfQues[k].empty()){ //push all elements in bin[k] until empty to a
/*int temp = arrayOfQues[k].front();
arr[i] = temp;
arrayOfQues[k].pop();
i++;*/
arr[i] = dequeue(arrayOfQues[k]);
}
}
currentDigit++;
}
}
Related
I have a question about linked list c++.
I know how to insert front one data, but I want to insert front multiple numbers that randomly created.
This is my push_front() function. I need to use this function to implement push_frontN() function.
please help me!!
struct Node{
int data;
Node* next;
Node(int i = 0; Node* n = nullptr){
data = i, next = n;
}
~Node() {}
};
int size(Node* p) {
if (empty(p)) return 0;
int count = 0;
for (pNode c = p; c != nullptr; c = c->next, count++);
return count;
}
Node* push_front(Node* p, int val){
if(empty(p)) return new Node{val}; //function empty is already created.
return new Node{val, p};
}
unsigned long rand_extended(int range) {
if (range < RAND_MAX) return rand();
return rand() * RAND_MAX + rand();
}
Node* push_frontN(Node* p, int N){
int range = N + size(p);
srand((unsigned)time(nullptr));
for (int i = 0; i < N; i++) {
int val = rand() % range;
p = push_front(p, val);
cout << setw(7) << "\r\tinserting in [" << i << "]=" << val;
}
cout << "\n";
return p;
}
Thank you
This is a piece of code for my project. I have such a program and I have a problem with filling an array in the graph. I think that the problem with new, but I don't understand what exactly. There is an error:
the expression must have an object pointer type and I don't know what to do with it. I'm a beginner in C++
I tried to use a . instead of -> but it was wrong
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
const int k = 3;
const int N = 1000;
struct Graph
{
struct Node *head[N];
};
struct Node
{
int dest;
int age;
string name;
string surname;
struct Node *next;
};
typedef Node* nodePtr;
struct Graph* createGraph(int );
void printGraph(struct Graph*);
void searchFor( int prefer[2*k][k], struct Graph* graph);
void createArray(struct Graph*, int arr[2*k][k]);
bool Preferences(int pref[2 * k][k], int w, int m, int m1);
int main()
{
int n;
int array[2 * k][k];
cout << "How many persons u want to have?" << endl;
cin >> n;
struct Graph *graph = createGraph(n);
printGraph(graph);
createArray(graph,array);
searchFor(array, graph);
return 0;
}
struct Graph* createGraph( int n)
{
int i,temp,j;
int b = n / 2;
int k, x = 0;
struct Graph *graph = new Graph;
for (i = 0; i < N; i++)
{
graph->head[i] = NULL;
}
srand(time(NULL));
for (i = 0; i < n; i++)
{
struct Node *newNode = new Node;
for ( k = 0; k < b; k++)
{
int m = b;
newNode->dest[k] = m;// here
b++;
}
for (int l = b; l < n; l++)
{
newNode->dest[l] = x; //here
x++;
}
for (int z = 0; z < b; z++)
{
if (z < b)
{
j = rand() % n + b;
temp = newNode->dest[z];
newNode->dest[z] = temp;
}
else
{
j = rand() % b + 0;
}
}
newNode->age = rand() % 90 + 1;
newNode->name = 'A' +(rand()%26);
newNode->surname = newNode->name = 'A' + (rand() % 26);
newNode->next = graph->head[i];
graph->head[i] = newNode;
}
return graph;
}
This statement:
newNode->dest[k] = m;
is illegal because dest was declared as an int. You can't use operator[] on an integer.
You have two main options to fix this:
Write newNode->dest = m; if you want to change the value of newNode->dest to m, or
Change dest to an integer array if you want multiple values to be held in it.
This is the part of the code where I'm facing the problem:
#include<iostream>
using namespace std;
struct ID{
int value;
ID *nxtElement;
};
struct SqLand{
ID *id;
SqLand *next;
};
int main(){
for(int p, i = 0; i < 3; i++){
SqLand *nptr = new SqLand;
cin >> p;
ID *buff = new ID;
buff = nptr->id;
for(int j = 0; j < p; j++){
cin >> buff->value;
//cout << "Garbage";
buff = buff->nxtElement;
}
buff = NULL;
//cout << "WILL";
delete nptr;
delete buff;
}
return 0;
}
The problem is that on running this program and inserting the value of p more than 1, the program exits after 2 more inputs.
For example, starting like this:
2
1
3
This is where the program exits
If both the cout statements are un-commented here are the outputs:
2
1
Garbage3
GarbageWILL
And another:
3
1
Garbage2
Garbage
All the programs exit after their respective last lines. What is the error in my program? It's a part of another program so that don't expect this snippet to make any sense. I only want to know where it goes wrong.
What i can understand from your code this ... (with fix)
struct ID {
ID(int val = -1, ID* _nxtElement = NULL) :
value(val), nxtElement(_nxtElement) {
}
int value;
ID *nxtElement;
};
struct SqLand {
SqLand(ID* _id = NULL, SqLand* _next = NULL) :
id(_id), next(_next) {
}
ID *id;
SqLand *next;
};
const int size = 3;
int main() {
SqLand* head = new SqLand;
SqLand* tmp = head;
for (int p, i = 0; i < size; i++) {
cin >> p;
tmp->id = new ID;
ID* tmpID = tmp->id;
for (int j = 0; j < p; j++) {
cin >> tmpID->value;
// avoid unnecessary allocate
if (j < p - 1) {
tmpID->nxtElement = new ID;
tmpID = tmpID->nxtElement;
}
}
// avoid unnecessary allocate
if(i < size - 1) {
tmp->next = new SqLand;
tmp = tmp->next;
}
}
return 0;
}
in your code you miss allocate for nptr->id and you miss the most important the head of list (SqLand *nptr = new SqLand;).
I have meet a problem that i cant debug my code. it keep showing me that unhandled exception.
#include <iostream>
#include <stdlib.h>
#include <ctime>
#define max 10
using namespace std;
struct Node{
int data;
Node *next;
};
void initNode(Node *tmpHead, int n){
tmpHead->data =n;
tmpHead->next =NULL;
}
void displayNodes(Node *cur) {
cout <<"Data is";
while(cur){
cout<< cur -> data << " ";
cur = cur->next;
}
cout << " *\n" <<endl;
}
void addNodes(Node * cur, int n){
Node *newNode = new Node;
newNode-> data = n;
newNode-> next = NULL;
while( cur -> next){
cur = cur -> next;
}
cur -> next = newNode;
}
int countTotalNodes(Node *cur){
int count =0;
while(cur){
count++;
cur = cur->next;
}
return count;
}
void main () {
srand(time(NULL));
Node* head = new Node;
int i =0;
int j= 0;
initNode ( head, rand() %100);
for ( int i = 0; i<max-1; i++)
addNodes( head, rand() % 100);
cout << endl;
cout << "Entered array is: " << endl;
displayNodes( head);
Node* array[max];
Node* cur= head;
Node* k;
for( int j = 0; j<max ; j++)
{
array[i] = cur;
cur = cur->next;
}
for (int i=0; i<max-1; i++) //sorting
{
for(int j=0; j<max-i-1; j++)
{
if(array[j]->data > array[j+1]->data)
{
k = array[j];
array[j] = array [j+1];
array[j+1] = k;
}
}
}
head = array[0];
for (int i =0; i<max -1; i++)
array[i]->next = array[i+1];
array[max-1]->next = NULL;
cout <<"Sorted Array is: " <<endl;
displayNodes( head);
}
I have found the part which i cant run it
if(array[j]->data > array[j+1]->data)
i tried to retype it and it still giving me the same error.
tried a few different pc it giving me the same error or it crashed.
Your code says:
for( int j = 0; j<max ; j++)
{
array[i] = cur;
cur = cur->next;
}
But using i as the index to the array makes no sense because j is the loop variable so the array contains random pointers because it isn't initialized properly.
You should run you code in a debugger like I just did and step through the program and watch what it does - it makes finding these things much easier.
I have written a binary search like following. When I try to find 10, it's not showing me the result. What am I missing??
// BinarySearch.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void BinarySearch(int arr[],int value);
int * insertionshot(int arr[]);
int _tmain(int argc, _TCHAR* argv[])
{
int arr[10] = {1,2,3,10,5,9,6,8,7,4};
int value;
cin >> value ;
static int *ptr;// = new int[10];
ptr = insertionshot(arr);
BinarySearch(ptr,value);
return 0;
}
int * insertionshot(int arr[])
{
int ar[10];
for(int i =0;i < 10; i++)
{
ar[i] = arr[i];
}
int arrlength = sizeof(ar)/sizeof(ar[0]);
for(int a = 1; a <= arrlength -1 ;a++)
{
int b = a;
while(b > 0 && ar[b] < ar[b-1])
{
int temp;
temp = ar[b-1];
ar[b-1] = ar[b];
ar[b] = temp;
b--;
}
}
return ar;
}
void BinarySearch( int a[],int value)
{
int min,max,middle;
min = 0;
int ar[10];
for(int i =0;i < 10; i++)
{
ar[i] = a[i];
}
//printf("size of array = %d",sizeof(arr));
max = (sizeof(ar)/sizeof(ar[0]) -1);
middle = (min+max)/2;
while(min <= max)
{
if(ar[middle] == value)
{
cout << "The value found" << ar[middle];
break;
}
else if(ar[middle] < value)
{
min = middle +1;
}
else if(ar[middle] > value)
{
max = middle-1;
}
middle = (min+max)/2;
}
}
Finally i made it work,I think this code does not have any problem.This could help any one
// BinarySearch.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void BinarySearch(int arr[],int value);
int * insertionshot(int arr[],int);
int _tmain(int argc, _TCHAR* argv[])
{
int arr[10] = {1,2,3,10,5,9,6,8,7,4};
int * arr1 = new int[10];
for(int i = 0;i< sizeof(arr)/sizeof(arr[0]);i++)
{
arr1[i] = arr[i];
}
int value;
cin >> value ;
int *ptr = new int[10];
ptr = insertionshot(arr1,10); // address of sorted array will be returned.
BinarySearch(ptr,value);
arr1 = 0;
ptr =0;
delete arr1;
delete ptr;
return 0;
}
int * insertionshot(int arr1[],int n)
{
for(int a = 1; a <= n -1 ;a++)
{
int b = a;
while(b > 0 && arr1[b] < arr1[b-1])
{
int temp;
temp = arr1[b-1];
arr1[b-1] = arr1[b];
arr1[b] = temp;
b--;
}
}
return arr1;
}
void BinarySearch( int a[],int value)
{
int min,max,middle;
min = 0;
int ar[10];
for(int i =0;i < 10; i++)
{
ar[i] = a[i];
}
max = (sizeof(ar)/sizeof(ar[0]) -1);
middle = (min+max)/2;
while(min <= max)
{
if(ar[middle] == value)
{
cout << "The value found" << ar[middle];
break;
}
else if(ar[middle] < value)
{
min = middle +1;
}
else if(ar[middle] > value)
{
max = middle-1;
}
middle = (min+max)/2;
}
}
You're missing the most important part of a binary search: The collection you search in must be sorted.
For binary search, the array should be arranged in ascending or descending order.