Merging numbers to form a big number - c++

If I have an int array with elements {1,2,3,4}
and I do not want to add the ints inside it but instead I want to have an int variable that holds the first 3 digits of the array for it to be int x = 123; and 123 being the first 3 elements{1,2,3} of the array, any ideas?
Basically instead of having 1 2 3 as separate digits in 3 separate indexes of the array I'd like to have 1 index OR variable that will be 123 as an actual 3 digit number.

You can use this approch. First of all assign the first digit of array (stored at array[0]) to the variable x. Then multiply it with 10 and then add the next digit of array into it, and do it one more time to insert first three digits into variable x.
Here is the code and hope it helps
#include<iostream>
int main()
{
int array[] = {1,2,3,4}, x = 0, i;
for(i=0;i<3;i++)
{
x = (x * 10) + array[i];
}
cout<<x;
return 0;
}

Related

How do I put the induvidual digits from 0 to 9 from a bigger number into an array containing only digits

I am trying to push all the individual digits of BigNumber into an array called IndividualNumber. (See code below.) Somehow the code that I try to use doesn't work. It doesn't push the digits into the array. Can someone please explain me why not?
int BigNumber = 2639;
array IndividualNumber;
for (int i = 0; i < 10; i++) {
IndividualNumber.push(BigNumber[i]);
}
//IndividualNumber should be [2, 6, 3, 9].
Thank you in advance and have a nice day.
There are a few problems with this code:
BigNumber is an integer but you are trying to index it like an array or pointer. One way to get the digits of a number in base 10 is to take the remainder when divided by powers of 10.
In C++ (which is used with Arduino), arrays have to be declared with a type and a capacity. The correct way to declare IndividualNumber with a capacity of 10 numbers for example would be something like:
int Individual[10];
To set i-th element of the array you use the following syntax:
IndividualNumber[i] = ...
With these corrected, a possible solution might look something like:
int BigNumber = 2639;
int IndividualNumber[10];
int temp = BigNumber;
for (int i = 0; i < 10; i++) {
int digit = temp % 10; // Remainder on division by 10
temp = temp / 10;
IndividualNumber[i] = digit;
}
This will store up to 10 digits of a number in IndividualNumber, in reverse order.

Does anyone know how to solve problems on variable length arrays?

Input Format
The first line contains two space-separated integers denoting the respective values of (the number of variable-length arrays) and (the number of queries).
Each line of the subsequent lines contains a space-separated sequence in the format k a[i]0 a[i]1 … a[i]k-1 describing the -element array located at.
Each of the subsequent lines contains two space-separated integers describing the respective values of (an index in the array ) and (an index in the array referenced by ) for a query.
Output Format-
For each pair of and values (i.e., for each query), print a single integer denoting the element located at an index of the array referenced by. There should be a total of lines of output.
Sample Input
2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3
Sample Output
5
9
Somebody has solved this problem by -
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,q; //n number of variable lenght arrays
// q no of queries asked
cin >>n >>q;
int ** Vectors = new int *[n];//no of length of var. arrays
int j;
for (int i=0;i<n;i++)
{
cin>>j;
Vectors[i] = new int [j];
for (int y=0;y<j;y++)
cin>>Vectors[i][y];
}
int q1,q2;
for (int i=0;i<q;i++)
{
cin >>q1 >> q2;
cout<<Vectors[q1][q2]<<endl;
}
return 0;
}
Can somebody explain me this code? Or if anyone has a better approach to solve this problem. Then please explain it in detail.
This shouldn't be hard to understand, that code is basically initializing dynamic 2D array at run time then inserting values to the 2D array and then accessing it by giving index:
int ** Vectors = new int *[n];//no of length of var. arrays
int j;
for (int i=0;i<n;i++)
{
cin>>j;
Vectors[i] = new int [j]; // initialzing inner array.. consider it as 2D array with n rows and j columns
for (int y=0;y<j;y++)
cin>>Vectors[i][y]; // insert element at specified index
}
cout<<Vectors[q1][q2]<<endl; // access element from 2D array
What you might want to use is a Matrix class.
Using
vector<vector<int>>
should do it.
Alternatively the snipet code should be refactored into a Matrix class with a constructor and a destructor.
The example you give present a memory leak since the allocated memory is not freed.

Query based shifting elements in array

We are given two numbers n and m. n indicates the number of elements in the array and m indicates number of queries.We are given m queries.We need to perform two types of queries on the array.Queries can be of two types, type 1 and type 2.
TYPE 1 queries are represented as (1 i j ) : Modify the given array by removing elements between i to j position and adding them to the front.
TYPE 2 queries are represented as (2 i j ) : Modify the given array by removing elements between i to j position and adding them to the back.
Our task is to simply print the difference array[1]-array[n] after the execution of queries followed by printing the array.
INPUT FORMAT:
First line consists of two space-separated integers, n and m.
Second line contains n integers, which represent the elements of the array.
m queries follow. Each line contains a query of either type 1 or type 2 in the form (type i j).
OUTPUT FORMAT:
Print the absolute value a[0]-a[n] in the first line.
Print elements of the resulting array in the second line. Each element should be separated by a single space.
EXAMPLE:
Given array is [1,2,3,4,5,6,7,8].
After execution of query(1 2 4),the array becomes(2,3,4,1,5,6,7,8).
After execution of query(2 3 5),the array becomes(2,3,6,7,8,4,1,5).
After execution of query(1 4 7),the array becomes(7,8,4,1,2,3,6,5).
After execution of query(2 1 4),the array becomes(2,3,6,5,7,8,4,1).
For the problem,I wrote a program as follows:
int main()
{
int n,m;
cin>>n;
cin>>m;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
int count; // counter to control no of queries to accept
for(count=0;count<m;count++)
{
int type,start,end; // 3 parts of query
cin>>type;cin>>start;cin>>end;
if(type==1)
{
//calculated difference between (start,end) to find no of iterations
for(int i=0;i<=(start-end);i++)
{ // t is temporary variable
int t=arr[(start-1)+i]; //(start-1) as index starts from 0
arr[(start-1)+i]=arr[i];
arr[i]=t;
}
}
else
{
for(int i=0;i<=(start-end);i++)
{
int t=arr[(start-1)+i];
// elements inserted from end so we subtract (n)-(start-end)
arr[(start-1)+i]=arr[(n-1)-(start-end)+i];
arr[(n-1)-(start-end)+i]=t;
}
}
count++;
//increment count
}
int absolute=abs(arr[0]-arr[n-1]);
cout<<absolute<<"\n";
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" "<<endl;
}
return 0;
}
I was expecting the code to work correctly,but surprisngly did not even display the output correctly as well.Here is the test case:
INPUT:
8 4
1 2 3 4 5 6 7 8
1 2 4
2 3 5
1 4 7
2 1 4
EXPECTED OUTPUT:
1
2 3 6 5 7 8 4 1
MY OUTPUT:
7
1
2
3
4
5
6
7
8
I had dry run the code many times but cannot seem to understand where the problem is coming from.Please look at the code and provide me suggestions.
The for loop condition is wrong.
CORRECT WAY : for ( int i = 0; i<=(end - start ) ; i++)

Incorrect output using array as counter

I'm trying to teach myself programming by attempting problems from codeabbey.com.
I'm not getting the correct output on this question.
Question:
Here is an array of length M with numbers in the range 1 ... N, where N is less than or equal to 20. You are to go through it and count how many times each number is encountered.
Input data contain M and N in the first line.
The second (rather long) line will contain M numbers separated by spaces.
Answer should contain exactly N values, separated by spaces. First should give amount of 1-s, second - amount of 2-s and so on.
Data input:
10 3
1 2 3 2 3 1 1 1 1 3
Correct Output:
5 2 3
My Output:
7 3 4
You can check here
My Code:
#include <iostream>
using namespace std;
int main()
{
int arrayLength,range,a;
cin>>arrayLength>>range;
int array[20];
array[20]={0};
for(int i=0; i<arrayLength; i++)
{
cin>>a;
++array[a-1];
}
for(a=0; a<range; a++)
{
cout<<array[a]<<" ";
}
return 0;
}
There aren't any error messages or warnings. Also, if you have any suggestions for improving the code, that'd be nice.
int array[20];
array[20]={0};
is wrong, since it leave the array un-initialized and tries to initialize the 21st element (which is undefined behaviour btw, since your array has only 20 elements, remember that indexing starts from 0). Use
int array[20] = {0}; // this will initialize all elements to 0
and your code will work as expected. See here for more details regarding aggregate initialization in C++.
array[20]={0}; initializes the 21st element(non-existing) to 0.
So you have to use int array[20] = {0}; which will initialize all 20 elements to zero.
Also from your code, you are not storing the elements to an array. You are just incrementing the corresponding count when an input is read. If so, what is the need of initializing an array to max limit. Just declare the array as you need it. In your case,
int array[range] = {0};
It will initialize an array of three (range =3 here) elements.

Array of structures in C++:assignment of values to each structure in array

I have declared a structure st below with a struct variable arr[], an array of structs.
Im trying to assign the value 1 to the 'num' variable, and values 1 to 10 to 'val' variable of the first 10 locations of array arr[]. And value 2 to 'num' and values 1 to 10 to 'val' of the next 10 locations. But when i traced the code, it won't assign values to the respective num and val of the same array location. If i wanted to assign num=1 and val=4 to the 4th structure it would assign num=1 to val of 3rd structure and val=4 to num of 4th structure.
My query is not about array indices.
The problem is:
If i wrote the statements
arr[2].num=1;
arr[2].val=2;
({num,val})
The expected result is: arr[2]={1,2}
But the actual result is:
arr[1]={num,1}
arr[2]={2,val}
#include<iostream.h>
#include<conio.h>
class abc
{
public:
struct st
{
int num;
int val;
};
st arr[21];
void funct();
};
void abc::funct()
{
int i,j,k=1;
for(i=1;i<=2;i++)
{
for(j=1;j<=10;j++)
{
arr[k].num=i;
arr[k].val=j;
k++;
}
}
}
int main()
{
abc z;
z.funct();
return 0;
}
1) Arrays are 0 based, i.e. index starts with 0 and goes upto arraySize - 1 (from your declaration).
2) Walk through your code and look at what each line is doing...
3) Now think about what you need to do:
3.1) iterate over each element of the array
3.2) for each element access the structure
3.3) inside the structure you wanted to set
num = 1 to the first 10 elements and 2 to second 10 elements (do you see any simple mathematical rule here?)
val = arrayElementIndex (this is too simple)
Look at your code and think about how it needs to be done.
http://www.cplusplus.com/doc/tutorial/arrays/
If you want to access the first element in the array named arr then you do it by arr[0].
So it will probably help if you do this:
int i,j,k=0;