Sorting, how should I cout the round and pass? - c++

Our professor told us to make our program cout the round and pass of the sorting algorithm.. What should I do then?
https://qph.is.quoracdn.net/main-qimg-21f7faf7e69c3317661f5f959008e031?convert_to_webp=true
Like this one ^
Bubble sort code
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
return 0;
}
Is this right??? --->
#include<stdio.h>
int main()
{
int a[100],c[100],b,n,i,temp;
printf("\t\t\tBUBBLE SORT\n");
printf("Enter total numbers : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter %d number : ",i+1);
scanf("%d",&a[i]);
c[i]=a[i];
}
printf("\n");
for(i=0;i<n;i++)
{
for(int j=0;j<n-1;j++)
{
b=0;
if(a[j]<a[j+1])
{ b++;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
if(b!=0)
{
for(int k=0;k<n;k++)
{
printf("%d ",a[k]);
}
printf("\n");
}
}
}
printf("\nAssending order By Bubble sort\n\t");
for(i=0;i<n;i++)
{
printf("%d ",c[i]);
}
return 0;
}

This is just simple step by step bubble sort problem.
just make two functions as bubble_asc(), bubble_desc()
and call it from the main function().
int main() {
...
bubble_asc();
bubble_desc();
...
}
void bubble_asc() {
...
do step by step bubble sort with ascending order here.
for(~~~~~) {
for(~~~~~~) {
if(array[a] < array[b])
....
}
}
print arrays here..
}
void bubble_desc() {
...
do step by step bubble sort with descending order here.
for(~~~~~) {
for(~~~~~~) {
if(array[a] > array[b])
....
}
}
print arrays here..
}

Related

generate a list of all possible combinations and randomly select a combination from the list

I found this code online on tutorials point. link https://www.tutorialspoint.com/cplusplus-program-to-generate-all-possible-combinations-out-of-a-b-c-d-e.
I tried to think of how to modify it so that it would randomly a single combination from the generated list, but I'm haven't figured it out yet.
#include<iostream>
using namespace std;
void Combi(char a[], int reqLen, int s, int currLen, bool check[], int l)
{
if(currLen > reqLen)
return;
else if (currLen == reqLen) {
cout<<"\t";
for (int i = 0; i < l; i++) {
if (check[i] == true) {
cout<<a[i]<<" ";
}
}
cout<<"\n";
return;
}
if (s == l) {
return;
}
check[s] = true;
Combi(a, reqLen, s + 1, currLen + 1, check, l);
check[s] = false;
Combi(a, reqLen, s + 1, currLen, check, l);
}
int main() {
int i,n;
bool check[n];
cout<<"Enter the number of element array have: ";
cin>>n;
char a[n];
cout<<"\n";
for(i = 0; i < n; i++) {
cout<<"Enter "<<i+1<<" element: ";
cin>>a[i];
check[i] = false;
}
for(i = 1; i <= n; i++) {
cout<<"\nThe all possible combination of length "<<i<<" for the given array set:\n";
Combi(a, i, 0, 0, check, n);
}
return 0;
}
im not a c++ specialist, but i think you should add a random number from -ArrayLenght to ArrayLenght, at least this works in python(which is written in c++)
i hope i understood your question right

Outputting differences in arrays

I have to make a code where the user inputs altitude readings and the code is supposed to output total climb, total descent, and net change. This is what I have below. I can't figure out how to code to have it output what I want it to.
#include <iostream>
using namespace std;
int main()
{
int array[2010], n, c, d, swap; //the array
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c=0; c < n; c++)
scanf("%d", &array[c]);
for (c=0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n"); //lists in order
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
// Returns minimum difference between any pair
int findMinDiff(int arr[2010], int n); //supposed to find differce
{
// Initialize difference as infinite
int diff = INT_MAX;
// Find the min diff by comparing difference
// of all possible pairs in given array
for (int d=0; d<n-1; d++)
for (int j=d+1; j<n; j++)
if (abs(array[d] - array[d--]) < diff)
diff = abs(array[d] - array[d--]);
cout<<"Total Climb "<<diff<<endl;
}
system("pause");
return 0;
}
I don't see why you are sorting the array. Sorting the array may cause problems in calculating the "total climb" and "total descent".
My understanding is that this assignment is about calculating the difference between two numbers and processing that difference.
void Process_Data(int array[2010], unsigned int quantity_of_climbs)
{
int total_climb = 0;
int total_descent = 0;
int minimum_change = INT_MAX;
for (int i = 0; i < quantity_of_climbs - 1; ++i)
{
const int height_change = array[i] - array[i+1];
if (height_change > 0) // A "climb"
{
total_climb += height_change;
}
if (height_change < 0) // A "descent"
{
total_descent = (-1) * height_change; // Change from negative to positive.
}
const int abs_height_change = abs(height_change);
if (abs_height_change < minimum_change)
{
minimum_change = abs_height_change;
}
}
// Display results
}

Intersection of 2 sets

I wrote a function in C++ for finding an intersection and union of 2 sets.
I tried this following function:
intersection function:
bool member (int x[10],int s[10])
{
bool result=false;
for (int i=0; i<10; i++)
for (int j=0 ; j<10;j++)
if (x[i]==s[j])
result=true;
return result;
}
int intersection(int s1[10],int s2[10],int s3[10])
{
for ( int i=0 ; i<10 ; i++)
{
if (member(s1,s2)==true)
s3[i]=s1[i];
}
return 0;
}
You can try union and intersection of two sets of user input size as below :
#include<stdio.h>
#include<malloc.h>
void main()
{
int *a, *b, *c, *d, n1, n2, i, j, k=0, l=0, flag=0;
printf("Enter number of elements in A and B : ");
scanf("%d %d",&n1,&n2);
a = (int *)malloc(sizeof(int)*n1);
b = (int *)malloc(sizeof(int)*n2);
c = (int *)malloc(sizeof(int)*((n1>n2)?n1:n2));
d = (int *)malloc(sizeof(int)*(n1+n2));
printf("\nEnter element in set A : ");
for(i=0;i<n1;i++)
{
printf("\nEnter element : ");
scanf("%d",&a[i]);
}
printf("\nEnter elements in set B : ");
for(i=0;i<n2;i++)
{
printf("\nEnter element : ");
scanf("%d",&b[i]);
}
for(i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(a[i]==b[j])
{
c[l++]=a[i];
j=0;
break;
}
}
}
for(i=0;i<n2;i++)
d[k++]=a[i];
for(i=0;i<n1;i++)
{
flag=0;
for(j=0;j<n2;j++)
{
if(b[i]==a[j])
{
flag=1;
break;
}
}
if(!flag)
d[k++]=b[i];
}
printf("\n\t A = {");
for(i=0;i<n1;i++)
{
printf("%d",a[i]);
}
printf("}");
printf("\n\t B = {");
for(i=0;i<n2;i++)
{
printf("%d",b[i]);
}
printf("}");
printf("\n\t A U B = {");
for(i=0;i<k;i++)
{
printf("%d",d[i]);
}
printf("}");
printf("\n\t A n B = {");
for(i=0;i<l;i++)
{
printf("%d",c[i]);
}
printf("}");
}
You're copying s1 in s3, one item at a time, if member(s1, s2) return true, that is if it's an intersection between s1 and s2
You should rewrite in this way (caution: not tested)
bool member (int x, int s[10])
{
bool result=false;
for (int j=0 ; (! result) && (j<10);j++)
if (x==s[j])
result=true;
return result;
}
int intersection(int s1[10],int s2[10],int s3[10])
{
int j = -1;
for ( int i=0 ; i<10 ; i++)
{
if (member(s1[i],s2)==true)
s3[++j]=s1[i];
}
// what about other places of s3?
return 0;
}

spiral matrix using c program

I have written a program to print the matrix in the spiral form, but it only works for 3*3. how to make it useful for all the dimensions.
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,k=1,l=0;
int n;
clrscr();
cout<<"Enter the number of row : ";
cin>>n;
int a[3][3];
cout<<"Matrix Form : "<<"\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=i*n+(j+1);
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
cout<<"Spiral form"<<"\n";
for(i=k;i<n;i++)
{
cout<<a[k][i]<<"\t";
}
k++;
for(i=k;i>=0;i--)
{
cout<<a[k][i]<<"\t";
}
k--;
for(i=k;i>=0;i--)
{
cout<<a[i][l]<<"\t";
}
for(i=k;i<n;i++)
{
cout<<a[l][i]<<"\t";
}
getch();
}
Assuming the code is working, you need to act on this row:
int a[3][3];
The easiest way is using C++ std::vector:
std::vector<std::vector<int> > a(n, std::vector<int>(n));
Remember to #include <vector> as well.
I tried to generate 3*3 spiral matrix using c by using increment and decrement of rows and columns in an array,then found out sum of main and opposite diagonals of the matrix.
My code is as follows
#include<stdio.h>
#define n 3
int main()
{
int m=n/2,i,j,a[10][10],c=1,sum=0,s=0;
for(i=m,j=m;j<n;j++)
{
a[i][j]=c;
c++;
}
j--;
i++;
while(j>=0)
{
a[i][j]=c;
c++;
j--;
}
i--;
j++;
a:
if(i>=0)
{
a[i][j]=c;
c++;
i--;
goto a;
}
i++;
j++;
b:
if(j<n)
{
a[i][j]=c;
c++;
j++;
goto b;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",a[i][j]);
}printf("\n");
}
for (i = 0; i <n; i++)
{
sum = sum + a[i][i];
s=s+a[i][n-i-1];
}
printf("sum of diagonals is d1: %d d2: %d d1+d2: %d",sum,s,s+sum);
return 0;
}
output:
click here to view output

Merge sort throwing index out of bounds exception in Xcode

I am trying to implement a simple merge sort for even number of elements using the following code :
#include <iostream>
using namespace std;
void merge(int arr1[10],int len1,int arr2[10],int len2,int arr3[10],int len3)
{
int i,j,k;
while(i<len1&&j<len2&&k<len3)
{
if(arr1[i]<arr2[j])
{
arr3[k] = arr1[i];
i++;k++;
}
else if(arr1[i]>arr2[j])
{
arr3[k] = arr2[j];
k++;
j++;
}
}
}
void mergeSort(int a[10],int n)
{
int arr1[10],arr2[10];
for(int i=0;i<n/2;i++)
{
arr1[i] = a[i];
}
for(int i=(n/2+1);i<n;i++)
{
arr2[i] = a[i];
}
mergeSort(arr1,n/2);
mergeSort(arr2,n/2);
merge(arr1,n/2,arr2,n/2,a,n);
}
int main() {
int arr[10],n;
cout << "Enter number of elements\n";
cin >> n;
cout<<"Enter elements\n";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
mergeSort(arr, n);
cout<<"Sorted array is"<<endl;
for(int i=0;i<n;i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
But I am getting a EXC_BAD_ACESS error on the opening brace of the mergeSort() method. I am new to Xcode and not sure on how to fix this. How do I fix this ?
Thanks !
You have forgot to initialize the int variables i, j, k to 0 (zero) in the function definition for merge(...).
void merge(int arr1[10],int len1,int arr2[10],int len2,int arr3[10],int len3)
{
// int i,j,k; // Not initialized.
int i = j = k = 0;
while(i<len1&&j<len2&&k<len3)
{
...
...
}
After Edit:
void mergeSort(int a[10],int n)
{
int arr1[10],arr2[10];
for(int i=0;i<n/2;i++)
{
arr1[i] = a[i];
}
// Here you are skipping the middle index.
// Let's say array is of length 10, then arr1 contains elements on index from 0 to 4 (5 elements),
// whereas arr2 contains elements on index from 6 to 9 (4 elements).
// 5th index element is missing.
// for(int i=(n/2+1);i<n;i++)
for(int i=(n/2);i<n;i++)
{
arr2[i] = a[i];
}
mergeSort(arr1,n/2);
mergeSort(arr2,n/2);
merge(arr1,n/2,arr2,n/2,a,n);
}
Hope it helps!