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

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.

Related

Want to specify a specific character in a char array

I have made a char array, where I basically enter the number of rows and columns, and enter in the letters for each row and column, such as x and y. Basically, like an normal array but with letters. After I enter the letters and finish with the array, I want to enter 2 numbers to specify a specific char in the array. For example, my array is 3x3. I fill my array with the characters x and y. After I do that, I enter 2 2. At row 2 and column 2, the letter there is y. So I want to print out y.
Here is my code.
#include <iostream>
using namespace std;
int main()
{
int x,y;
cin>>x>>y;
char n[x][y];
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
cin>>n[i][j];
}
}
int a,b;
cin>>a>>b;
cout<<n[a][b];
}
The main problem with your given program is that in Standard C++ the size of an array must be a compile time constant. This means that the following is not valid:
int x,y;
cin>>x>>y;
char n[x][y];//not standard C++ because x and y are not constant expressions
To solve this you can use a std::vector<char> as shown below:
#include <iostream>
#include<vector>
int main()
{
int x,y;
std::cin>>x>>y;
//create a 2D vector
std::vector<std::vector<char>> n(x, std::vector<char>(y));
//iterate through rows
for(auto& row: n)
{
//iterate through each element in the row
for(auto& col: row)
{
std::cout<<"enter element: "<<std::endl;
std::cin>>col; //take input from user
}
}
int a,b;
std::cout<<"a and b"<<std::endl;
std::cin>>a>>b;
std::cout<<"element at position: "<<a<<" "<<b <<" is: "<<n[a][b];
}
Working demo

Having trouble with converting this one dimensional array to a two dimensional array in 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];
}

The output of this question is correct but i am getting a segmentation error

I am trying to solve the program of array rotation. I am getting segmentation error in the code. Can someone please tell where is the problem in this code?
this is the question
Given an unsorted array arr[] of size N, rotate it by D elements (clockwise).
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.
and i have solved it with the following code.
#include <iostream>
using namespace std;
int* rotate(int ar[],int n, int m)
{static int temp[100];
for(int i =0;i<m;i++)
{
temp[i]=ar[i];
}
for(int j =m;j<n;j++)
{
ar[j-m]=ar[j];
}
int x=0;
for(int k =n-m;k<n;k++)
{
ar[k]=temp[x];
x++;
}
return ar;
}
int main() {
//code
int t, n , m;
cin>>t;
while(t>0)
{
cin>>n>>m;
int arr[n];
int * ptr;
for(int i = 0 ;i<n;i++)
{
cin>>arr[i];
}
ptr=rotate(arr,n,m);
for(int j=0;j<n;j++)
cout<<ptr[j]<<" ";
cout<<endl;
t--;
}
return 0;
}
If m > n then it crashes in the first for() loop in rotate as you index past the end of arr.
If m < 0 it crashes in the 2nd loop as it index before arr.
There are probably more cases.

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.