Sorting an array from lowest to greatest - c++

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.

Related

I am trying to pass an DMA array and its size as an argument but it is giving an error

I am trying to pass a dynamic memory allocated array and its size to a function 'sum' but it is giving error of permissive what should I do?
#include<conio.h>
#include<iostream>
using namespace std;
int sum(int n[], int *m)
{
for(int z=0;z<*m;z++)
{
cout<<"\n the output is = "<<n[z]<<"\n";
}
}
int main()
{
int *n,*m,a; //declaration is done here**strong text**
cout<<"enter the size of array = ";
m=new int;
cin>>*m;
n=new int[*m];
for(int i=0;i<*m;i++)
{
cout<<"\n enter the "<<i+1<<" array = ";
cin>>n[i];
cout<<"\n";
}
/* for(int z=0;z<*m;z++)
{
cout<<"\n the output is = "<<n[z]<<"\n";
}*/
int sum(n,&m);//here "m" is an pointer and I am trying to pass int in a function with an array
return 0;
}
Your code should, probably, look like the following (Linux Ubuntu + gcc):
#include <iostream>
using namespace std;
int sum(int n[], int m)
{
int s=0;
for(int z=0; z<m; z++)
{
cout<<"\n array["<<z<<"]= "<<n[z]<<"\n";
s+=n[z];
}
return s;
}
int main()
{
int *n,m;
cout<<"enter the size of array = ";
cin>>m;
n=new int[m];
for(int i=0; i<m; i++)
{
cout<<"\n enter array["<<i+1<<"] value = ";
cin>>n[i];
cout<<"\n";
}
int s = sum(n, m);
cout<<"s="<<s<<endl;
return 0;
}
There is no use allocating the size of the array m dynamically. It is an ordinary int variable and can be initialized as
cin>>m;
You may also write the sum prototype in the form
int sum(int * n, int m)
It is another way of passing a 1-dimensional array as a function parameter.
Speaking frankly, these questions are the very basics of the language.
You should, probably, read something like
Dynamic memory allocation/dynamic arrays
about dynamic memory allocation and dynamic arrays and
Simple cases of std::cin usage
about the simplest cases of std::cin usage in C++.

What is the error in this bubble sort code?

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.

C++ program automatically rounding off double values except the first value in an array

I am using this code for the function overloading using C++ to sort a double and an integer array.
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
void sort(int arr[],int n)
{
int i,j,key;
for(j=1;j<n;j++)
{
key=arr[j];
i=j-1;
while((i>=0)&&(arr[i]>key))
{
arr[i+1]=arr[i];
i--;
}
arr[i+1]=key;
}
cout<<"Sorted integer array is \n";
for(i=0;i<n;i++)
{
cout<<arr[i]<<endl;
}
}
void sort(double arr[],int n)
{
int i,j,key;
for(j=1;j<n;j++)
{
key=arr[j];
i=j-1;
while((i>=0)&&(arr[i]>key))
{
arr[i+1]=arr[i];
i--;
}
arr[i+1]=key;
}
cout<<"Sorted double array is \n";
for(i=0;i<n;i++)
{
cout<<arr[i]<<endl;
}
}
int main()
{
int n;
cout<<"Enter the size of the array \n";
cin>>n;
cout<<"Enter the values for the integer function \n";
int a[n];
int i;
for(i=0;i<n;i++)
{
cin>>a[i];
}
double b[n];
cout<<"Enter the values for the double array \n";
for(i=0;i<n;i++)
{
cin>>b[i];
}
sort(a,n);
sort(b,n);
}
In the answer it rounds off the values of the double values except the first entry into the double array. Is there any type casting step which I am missing or any other flaw in the code?
You are irrevocably cutting off an floating part of double if you save it to int variable.
int key;
key=arr[j];
Thats why are you getting wrong results.
Declare key as double and it works.
Also VLAs (variable length arrays) are not standart C++.
Here are some other variants
std::array
std::vector
use new & delete

How do I find the mode of an array by passing the pointer of that array to a function? C++

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

sorting array of struct by id

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)