Having trouble with converting this one dimensional array to a two dimensional array in C++ - dev-c++

This is a program that asks a user to input 20 numbers then arranges them in a 2-dimensional array with ascending order. I'm having trouble with converting the 1 dimensional array into a 2 dimensional array. The output results into a two dimensional array with the same values.
#include<iostream>
#include<iomanip>
using namespace std;
//global
int i,j,count;
int row,col;
const int sizeRow=4;
const int sizeCol=5;
int list[sizeRow][sizeCol];
int elements[20];
int temp;
int main()
{
cout<<"Processing Bubble Sorting Technique..."<<endl
<<"Enter 20 elements"<<endl;
for (i=0;i<20;i++) //input
{
cout<<"loc["<<i<<"]:";
cin>>elements[i];
cout<<endl;
}
for (i=0;i<20;i++) //process-bubble sorting
{
for (j=i+1;j<20;j++)
{
if (elements[j]<elements[i])
{
temp=elements[i];
elements[i]=elements[j];
elements[j]=temp;
}
}
}
//converting 1 dimensional array to 2 dimensional
cout<<"Ascending Order"<<endl;
for (count=0;count<20;count++)
{
for (i=0;i<sizeRow;i++)
{
for(j=0;j<sizeCol;j++)
{
list[i][j]=elements[count];
}
}
}
//output
for (i=0;i<sizeRow;i++)
{
for(j=0;j<sizeCol;j++)
{
cout<<left<<setw(5)<<list[i][j]<<" ";
}
cout<<endl;
}
}

It's your triply-nested loop. Think about it; that going to assign EACH element to EVERY element of the outgoing array.
You want:
for (count=0;count<20;count++)
{
int i = count / sizeCol;
int j = count % sizeCol;
list[i][j]=elements[count];
}

Related

getting error while trying to rotate array clock wise using stack

Given an unsorted array arr[] of size N, rotate it by D elements (clockwise).
Input
The first line of the input contains T denoting the number of testcases. First line of each test case contains two space separated elements, N denoting the size of the array and an integer D denoting the number size of the rotation. Subsequent line will be the N space separated array elements
Output
For each testcase, in a new line, output the rotated array
Example
Input
1 2 3 4 5
Output
3 4 5 1 2
#include <iostream>
#include <stack>
using namespace std;
void rotate(int *a,int s,int r) {
stack<int> st;
for(int i=0;i<r;i++) {
st.push(a[i]);
}
for(int j=r;j<s;j++) {
a[j-r] = a[j];
}
for(int k=s-1;k>r+1;k--) {
a[k] = st.top();
st.pop();
}
for(int l=0;l<s;l++) {
cout<<a[l]<<" ";
}
}
int main() {
//code
int T;
cin>>T;
while(T--) {
int N,r;
cin>>N>>r;
int A[N];
for(int i=0;i<N;i++) {
cin>>A[i];
}
rotate(A,N,r);
cout<<endl;
}
return 0;
}
I followed your logic, it seems like there is problem in your backfill part.
for(int k=s-1;k>=s-r;k--) { // change k>r+1 to k>=s-r
a[k] = st.top();
st.pop();
}
sorry my bad, int third for loop in rotate function there should be k>s-r-1

determine if the array could be sorted rotating 3 consecutive array elements?

I have a permutation of a sequence of natural numbers incrementing from 1 as an array. How can I determine whether the array can be sorted using rotation of 3 consecutive elements?
I have implemented an algorithm in which basically I'm comparing the indices of the array with the element at that index in the array. If they are not equal then I call the function choose_indices() which first finds the element to be swap at the right position in the array and after finding it, selects the 3 consecutive elements including the number to be swapped and rotates them. After performing n-1 rotations for an array of size n, the array is sorted. This implementation returns true if an array can be sorted using 3 consecutive element rotation but timeouts for an array if the array can't be sorted using this method.
using namespace std;
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstring>
int n;
void rotate(vector<int> &arr,int end,int mid,int start)
{
int temp=arr[start];
arr[start]=arr[end];
arr[end]=arr[mid];
arr[mid]=temp;
}
void choose_indices(vector<int> &arr,int s,int q)
{
for(int l=q;l<n;l++)
{
if(arr[l]==s)
{
if(l-q>=2)
{
rotate(arr,l,l-1,l-2);
break;
}
else
{
rotate(arr,l+1,l,l-1);
break;
}
}
}
}
int main()
{
vector<int> arr;
int q,count=0;
cin>>q;
for(int i=0;i<q;i++)
{
cin>>n;
count=0;
for(int i=0,p;i<n;i++)
{
cin>>p;
arr.push_back(p);
}
for(int j=0,k=1;j<n && k<n; )
{
if(arr[j]!=k)
{
choose_indices(arr,k,j);
if(arr[j]==k)
{
j++;
k++;
count++;
}
}
else
{
j++;
k++;
count++;
}
}
if(count==n-1)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
arr.clear();
}
}
Sample Input:
1 2 3 5 4
For this input, my code gives runtime error.
How can I find if the array given cannot be sorted using the rotation of 3 consecutive elements?
Rotating 3 adjacent elements will always cancel 2 inversions if present or it will introduce 2 inversions.
Consider this:
1 2 3 5 4
Has only 1 inversion, no matter how many times you rotate , you can never cancel that inversion without introducing other inversions as you will be always rotating 3 consecutive elements.
So just count the number of inversions and if its odd then the answer is NO, otherwise YES. There are efficient algorithms out there to count the number of inversions(like merge sort).
using namespace std;
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstring>
int n;
void rotate(vector<int> &arr,int end,int mid,int start)
{
int temp=arr[start];
arr[start]=arr[end];
arr[end]=arr[mid];
arr[mid]=temp;
}
void choose_indices(vector<int> &arr,int s,int q)
{
for(int l=q;l<n;l++)
{
if(arr[l]==s)
{
if(l-q>=2)
{
rotate(arr,l,l-1,l-2);
break;
}
else
{
rotate(arr,l+1,l,l-1);
break;
}
}
}
}
int main()
{
vector<int> arr;
int q,count=0;
cin>>q;
for(int i=0;i<q;i++)
{
cin>>n;
count=0;
for(int i=0,p;i<n;i++)
{
cin>>p;
arr.push_back(p);
}
//Counting the number of inversion in the array
int ctiv=0;
for(int r=0;r<n;r++)
{
for(int s=r+1;s<n;s++)
{
if(arr[r]>arr[s] && r<s)
{
ctiv++;
}
}
}
if(ctiv%2!=0)
{
cout<<"NO"<<endl;
}
else
{
for(int j=0,k=1;j<n && k<n; )
{
if(arr[j]!=k)
{
choose_indices(arr,k,j);
if(arr[j]==k)
{
j++;
k++;
count++;
}
}
else
{
j++;
k++;
count++;
}
}
if(count==n-1)
{
cout<<"YES"<<endl;
}
arr.clear();
}
}
}
This is the algorithm i designed which finds if the given algorithm can be sorted and if it can be sorted, it sorts the given array by performing the necessary 3 consecutive rotations.

How to array of int transform into 2d array of binary value of every array number?

Hi i'm trying to find binary value of every element of array and to set it in 2d array.
Here is my code:
#include <iostream>
void tobin(int a,int b[]);
int main()
{
int i,j;
int b[8];//bin array
int d[3][8]; //2d array
int c[3] = {6,15,24}; //int array
//My code for transformation from array to 2d array
for(i=0;i<3;i++)
{
tobin(c[i],b);
for(j=0;j<8;j++)
{
d[i][j]=b[j];
}
}
//Printing of 2d array
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
std::cout<<d[i][j];
std::cout<<std::endl;
}
}
//Function for convert int to bin
void tobin(int a,int b[])
{
int i;
for(i=0;i<8;i++)
{
b[i]=a%2;
a/=2;
}
}
And here my output:
01100000
11110000
00011000
00-16437331793270200193648149632765
014196397000-640061051-1985511146
419616001936481488327650000
16416403251985832910-46743922719940467470000
004196848019364814963276510
First 3 lines is what i was looking for, but i don't know what is the rest.
Can someone help me to fix this. And to give me explanation why is this happening or link to explanation.
Your last for-loop (the one that prints) should only have i<3 as its condition. Right now i is allowed to go to 7, but then d[i][j] is out of bounds and prints garbage.

I am trying to run the following code for quicksort but the output is always a garbage value.What should be the modification in the code?

this is the following code
#include<iostream>
using namespace std;
int findPivot(int a[],int startIndex,int endIndex)
{
int pivot=a[endIndex];
int pivotIndex=startIndex;
for(int i=0;i<endIndex-1;i++)
{
if(a[i]<pivot)
{
int temp=a[i];
a[i]=a[pivotIndex];
a[pivotIndex]=a[i];
pivotIndex++;
}
}
int temp=pivot;//swapping pivot element into its position.
pivot=a[pivotIndex];
a[pivotIndex]=temp;
return pivotIndex;
}
void quickSort(int a[],int startingIndex,int endingIndex)
{
int number;
if(startingIndex < endingIndex)
{
int returnValueOfPivot= findPivot(a,startingIndex,endingIndex);
//cout<<returnValueOfPivot<<endl;
quickSort(a,startingIndex,returnValueOfPivot-1);//sorting for left
quickSort(a,returnValueOfPivot+1,endingIndex);//sorting for right
}
}
int main()
{
int number;
cout<<"Enter the total number of elements"<<endl;
cin>>number;
cout<<"Enter the values"<<endl;
int a[number-1];
for(int i=0;i<number;i++)
{
cin>>a[i];
}
quickSort(a,0,number-1);
for(int i=0;i<number;i++)
{
cout<<a[i]<<",";
}
return 1;
}
There are three major problems in your code :
int a[number-1];
You are allocating 1 less space for your array. Note that, array index starts from 0. So array of 5 numbers will be like
array[5] : array[0],array[1],array[2],array[3],array[4]
Swapping array values :
int temp=pivot;//swapping pivot element into its position.
pivot=a[pivotIndex];
a[pivotIndex]=temp;
Here, you swapped pivot value with a[pivotIndex] not a[endIndex]!!
So the correct swap would have been :
int temp=a[endIndex];//swapping pivot element into its position.
a[endIndex]=a[pivotIndex];
a[pivotIndex]=temp;
for(int i=0;i<endIndex-1;i++) is incorrect loop
correct loop would be :
for(int i=startIndex;i<=endIndex-1;i++)
You need to start from the start index and end till the end index. You are currently going from 0 to end - 1. [Think of the right side array loop, it won't start with 0]
Make these changes and your code will work.

output is not what expected

#include<iostream>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
using namespace std;
int swapper(int n[],int s
)
{
for(int i=0;i<s;i++)
{
int temp=n[i];
n[i]=n[i+1];
n[i+1]=temp;
}
cout<<"\n Array Swapped !!!";
for (int i=0;1<s;i++)
{
cout<<n[i]<<"\t";
}
return 0;
}
int main()
{
int n[20]; int s;
cout<<"\n enter array size:";
cin>>s;
cout<<"enter no in array according to size given";
for(int j=0;j<s;j++)
{
cin>>n[j];
}
swapper(n,s);
return 0;
getch();
}
the output to this program does not swap the array elements,
instead it starts producing numbers in plenty
the entire code is written here
all other suggested changes have been made
the function is supposed to take in an integer array and its size as parameters and display an array with its adjacent elements swapped.
The call
swapper(n[],s);
is not right. Assuming the first argument of swapper is a int*, it needs to be:
swapper(n,s);
Remove the [] in your swapper function.