I take an array of record as case study in my Graduation Project. In the code I've written, I made an array of the structure and entered the id of the structs in an array
of int to make the sorting easier but when it runs it gets stuck.
#include<iostream.h>
struct book{
char Bname[8], Bsubject[8];
int copyies, Bid, Bfound;
} a[100];
void Ider(book a[],int r[],int index){
for(int i=0;i<index;i++)
r[i]=a[i].Bid;
}
void swap(int &a,int &b){
int c=a;
a=b;
b=c;
}
void Bublesort(int a[],int n){
for(int j=0;j<n;j++)
for(int i=1;i<n-1;i++)
if(a[i]>a[i+1])
swap(a[i+1],a[i]);
}
int bsearch(int b[],int key,int first,int last){
int midel=(first+last)/2;
for(int i=0;i<last;i++){
if(b[midel]==key)
return midel;
else
if(b[midel]>key)
return bsearch(b,key,first,midel-1);
else
if(b[midel]<key)
return bsearch(b,key,midel+1,last);
}
}
int main(){
int r,i, index=0,m[100];
char ch;
do{
cout<<"Enter your"<<index+1<<" Book id"<<" ";
cout<<endl;
cin>>a[index].Bid;
cout<<"Enter your"<<index+1<<" Book name"<<" ";
cout<<endl;
cin>>a[index].Bname;
cout<<"Enter your"<<index+1<<" Book subject"<<" ";
cout<<endl;
cin>>a[index].Bsubject;
index++;
a[index].Bfound++;
a[index].copyies++;
if(index==99)
break;
quite : cout<<"Do you want to continue";
cin>>ch;
} while(ch=='y');
Ider(a,m,index);
Bublesort(m,index);
char p;
do{
cout<<"Do you want to search?"<<endl;
cin>>p;
cout<<"enter your id";
cin>>i;
r=bsearch(m,i,0,index);
cout<<a[r].Bfound<<" ";
} while(ch=='y');
system("pause");
return 0;
}
When it reaches bublesort() it hangs and didn't give any output, but why?
Among other problems: a bubblesort goes over the array again and again until it's completely sorted. Yours only goes over the array once.
C example
Bubble sort (Wikipedia)
Related
I was trying to modify the selection sort code in c++ to check the results. My modified code is:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter number of elements\n";
cin>>n;
int i,j,small,pos,t;
int a[n];
cout<<"Enter elements of array\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n-1;i++)
{
small=a[i];
pos=i;
for(j=i+1;j<n;j++)
{
if(a[j]<small)
{
small=a[j];
pos=j;
}
t=a[i];
a[i]=a[pos];
a[pos]=t;
}
}
cout<<"Sorted array:\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
This code works for certain arrays but does not work for others.
For example: If I enter 1,11,2,22,3 as the array, I get a proper output: 1,2,3,11,22. But, if I enter 1,11,2,22,3,33 as the array, I get the same array as the input as the output. Please tell me what is wrong with my code.
The issue is because of wrong place of the swap logic. The swap should be placed outside the inner loop.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter number of elements\n";
cin>>n;
int i,j,small,pos,t;
int a[n];
cout<<"Enter elements of array\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n-1;i++)
{
small=a[i];
pos=i;
for(j=i+1;j<n;j++)
{
if(a[j]<small)
{
small=a[j];
pos=j;
}
}
//Swap here
t=a[i];
a[i]=a[pos];
a[pos]=t;
}
cout<<"Sorted array:\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
Now this gives the output as expected.
I wrote this simple code for bubble sort but it gives some random garbage values as output. Can someone please tell me my mistake. I tried to print the output of A[i] and A[j] in the function bubbleSort and looks like it is working fine. But why is the printSortedArray not giving the correct output? Thanks!
#include <iostream>
using namespace std;
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void printSortedArray(int A[],int size)
{
cout<<"the sorted array is"<<endl;
int i;
for(i=0;i<size;i++);
{
cout<<A[i]<<" ";
}
}
void bubbleSort(int A[],int size)
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size-1-i;j++)
{
if(A[j]>A[j+1])
{
swap(A[j],A[j+1]);
}
}
}
}
int main()
{
int A[50]; int size,i;
cout<<"enter the size of the array: ";
cin>>size;
cout<<"Enter the "<<size<<" numbers to be sorted"<<endl;
for(i=0;i<size;i++)
{
cin>>A[i];
}
bubbleSort(A,size);
printSortedArray(A,size);
return 0;
}
for(i=0;i<size;i++);
The trailing semicolon does not belong there. This results in undefined behavior.
The end result is that this function printed one garbage value after the end of the array.
So I'm totally new to pointers, I apologize for this, I'm supposed to pass an array of pointers and get the mode of that array. After the array passes as a set of pointers, I can't manipulate the array to find the mode, everything I try results in a syntax error.
EDIT: I changed list to an array of pointers and I get a runtime error.
int main()
{
int size=0;
int *list[size];
cout<<"Please enter the size of your array: ";
cin>>size;
cout<<"\nPlease enter the numbers in your list seperated by spaces: ";
for(int i=0;i<size;i++)
{
cin>>*list[i];
}
cout<<endl;
int mode=getMode(list,size);
cout<<"\n"<<mode<<endl;
return 0;
}
int getMode (int* list[], int arraySize)
{
cout<<"The array you entered is listed below\n "<<list[0];
for(int i=0;i<arraySize;i++)
{cout<<setw(3)<<list[i];}
int *number=list[0];
int count1=0;
int count2=0;
int mode=0;
for(int j=1;j<arraySize;j++)
{
for(int i=1;i<arraySize;i++)
{
if(list[i]==number)
{
count1++; //counts the number of instances that the number occurs
}
}
if(count1>count2)
{
mode= *list[j];
count2=count1;
}
count1=0;
}
return mode;
}
When you pass an array to a function, it automatically decays to a pointer, so you don't need to use &list. And in the function, you shouldn't declare it int *list[], it should just be int list[] or int *list.
Also, in the getMode() function, you need to count the matches of list[j]. You're just counting the repetitions of number, which is list[0].
#include <iostream>
#include <iomanip>
using namespace std;
int getMode (int list[], int arraySize)
{
cout<<"The array you entered is listed below\n "<<list[0];
for(int i=0;i<arraySize;i++)
{cout<<setw(3)<<list[i];}
int count1=0;
int count2=0;
int mode=0;
for(int j=0;j<arraySize;j++)
{
for(int i=0;i<arraySize;i++)
{
if(list[i]==list[j])
{
count1++; //counts the number of instances that the number occurs
}
}
if(count1>count2)
{
mode= list[j];
count2=count1;
}
count1=0;
}
return mode;
}
int main()
{
int size;
int *list;
cout<<"Please enter the size of your array: ";
cin>>size;
list=new int[size];
cout<<"\nPlease enter the numbers in your list seperated by spaces: ";
for(int i=0;i<size;i++)
{
cin>>list[i];
}
cout<<endl;
int mode=getMode(list,size);
cout<<"\n"<<mode<<endl;
return 0;
}
DEMO
I wanted to pass an array pointer to a function and show content of that array from that function.I did this in below way but when i run the code the whole program is crashing.Please help me or give me idea how to solve this problem...
int main()
{
int size,i;
cout<<"Please enter the size of the array";
cin>>size;
int *array_=new int [size];
cout<<"Please enter all elements of the array";
for(i=0;i<size;i++){
cin>>array_[i];
}
insertion(&array_,size);
return 0;
}
void insertion(int *array_[],int size){
int i;
for(i=0;i<size;i++){
cout<<*array_[i];
}
}
void insertion(int *array_,int size){
int i;
for(i=0;i<size;i++){
cout<<array_[i]<<" ";
}
}
So i'm in my first programming class and am having some trouble with an assignment. The assignment was to make a code that asks the user for six integers and six letters and to store them in arrays. Then print a bar graph with the data sorted from lowest to highest. That is the part i am having trouble with. I named it the Arrange function, and to me everything looks right. I was just hoping someone could tell me where i went wrong in swapping the lowest integer. Any other comments on the code would be much appreciated! Thanks (I also cant use sort functions from a library, or pointers)
#include <iostream>
using namespace std;
void Arrange(int num[],int order[],char let[],char letOrder[],int cap);
void Swap(int order[],char let[],int a,int b);
void BuildGraph(int number[],char letter[],int max);
int getMax(int number[]);
int main(){
int Number[6];
char Letter[6];
int Order[6];
char LetterOrder[6];
int max;
cout<<"Please Enter 6 Integers: "; //gets 6 integers
for(int i=0;i<6;i++){
cout<<"Ineteger # "<<i+1<<" = ";
cin>>Number[i];
cout<<endl;
}
cout<<"Please Enter 6 Characters: "; //gets 6 characters
for(int i=0;i<6;i++){
cout<<"Enter a character here: ";
cin>>Letter[i];
cout<<endl;
}
max= getMax(Number);
BuildGraph(Number,Letter,max);
Arrange(Number,Order,Letter,LetterOrder,max);
BuildGraph(Order,LetterOrder,max);
return 0;
}
int getMax(int number[]){
int max=number[0];
for(int i=0;i<6;i++){
if(max<number[i]) max=number[i];
}
return max;
}
void Arrange(int num[],int order[],char let[],char letOrder[],int cap){
int i,n;
order=num;
letOrder=let;
for(i=0;i<5;i++){
for(n=0;n<6;n++){
if(order[i]>order[n]){
Swap(order,letOrder,i,n);
}
}
}
cout<<order[4];
}
void Swap(int order[],char let[],int a,int b){
int temp1=order[b];
order[b]=order[a];
order[a]=temp1;
int temp2=let[b];
let[b]=let[a];
let [a]=temp2;
}
void BuildGraph(int number[],char letter[],int max){
cout<<"Bar Chart"<<endl;
for(int row=max;row>=1;row--){
for(int col=0;col<6;col++){
if(row<=number[col]) cout<<letter[col]<<" ";
else cout<<" ";
}
cout<<endl;
}
}
It would probably be easier to visualize and implement if you have separate functions for arrange and swap.
void arrangeNum(int num[])
void arrangeChar(char char[])
void swapInt(int x, int y)
void swapChar(char x, char y)
Then the arrange functions will just be bubble sorts. But you also lose scope when calling swap so they don't actually do anything. It would be easier to just swap them within the Arrange functions.