Interview Question
Country: United States
If we are interested in the final result, then the we can achieve the result without conversion. The code is given below
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[] = {7,3,5,3,9};
int i;
// Instead of adding 1 separately, set carry initially
int carry = 1;
int size = sizeof(a)/sizeof(a[0]);
int* result = (int*)malloc((size+1)*sizeof(int));
for(i=size-1;i>=0;i--)
{
result[i+1] = (a[i]+carry)%10;
carry = (a[i]+carry)/10;
}
result[0] = carry;
for(i=0;i<size+1;i++)
printf("%d",result[i]);
printf("\n");
return 0;
}
am i missing anything
<?php
$ss = array(2,3,4);
$count = count($ss);
$sum = 0;
foreach($ss as $key => $item){
$sum += $item * (10 to the power ($count-$key));
}
$final = $sum +1;
$count_integer = lenght($final);
for ($i = 0; $i < $count_integer; $i++){
$new_array[] = $final % 10;
$final = ceil($final/10);
}
?>
import java.util.Arrays;
public class ArrayToNumber {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//int [] inputArray = {9,9,9};
int [] inputArray = {7,3,5,3,9};
System.out.println( "out put:"+ Arrays.toString(inputNumber(inputArray)));
}
/**
*
For example input = {7,3,5,3,9} convert this to number 73539,
add 1 so it becomes 73540 and convert to array {7,3,5,4,0}.
Array can be of any length, so you can't always represent array in form
of in-built number format. So you have to do this summation in-place.
Also, how would you increase array size in-case input = {9,9,9}
so output = (1,0,0,0}
Assume, all elements of arrays are between 0 and 9.
*/
public static int [] inputNumber(int [] inputArray){
int integerValue=0;
int [] outputArray = new int[inputArray.length];
int [] overFlowOutputArray = null;
//Converting array of elements into integer number
for(int i=0; i<inputArray.length;i++){
integerValue=integerValue*10+inputArray[i];
}
//Adding one to the integer number which got from previous step
integerValue = integerValue+1;
//Converting integer number back array
for(int j=inputArray.length-1; integerValue>0;j--){
//Check the array need to be increased and return the array
if(j==-1){
overFlowOutputArray = new int[inputArray.length+1];
System.arraycopy(outputArray, 0, overFlowOutputArray, 1, inputArray.length);
overFlowOutputArray[0] = integerValue%10;
return overFlowOutputArray;
}
outputArray[j] = integerValue%10;
integerValue = integerValue/10;
}
return outputArray;
}
}
import java.util.Stack;
public class ArrayElementToNumber {
public static void main(String[] args) {
System.out.println("Enter the length of array");
int[] arr = { 7, 5, 3, 2, 9,5,2,3
};
Stack<Integer> stack = new Stack<Integer>();
int i = 0;
int n = 1;
while (i <= arr.length - 1) {
stack.push(arr[arr.length - 1 - i] * n);
n = n * 10;
i++;
}
int x = 0;
int result = 0;
while (x < stack.capacity()) {
int number = stack.pop();
result = number + result;
System.out.println(result);
}
}
}
#include<stdio.h>
#include<conio.h>
int main(){
int n,a[20],b[20],i,count=0;
long num=0,num1;
clrscr();
printf("Enter number of elements in the array: ");
scanf(" %d",&n);
for(i=0;i<n;i++){
printf("Enter the %d element of array: ",i+1);
scanf(" %d",&a[i]);
num=num*10+a[i];
}
printf("\nThe elements inside the array is: ");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
num=num+1;
num1=num;
while(num/10!=0){
count=count+1;
num=num/10;
}count++;
n=count;
for(i=count-1;i>=0;i--){
b[i]=num1%10;
num1=num1/10;
}
printf("\nThe new array is: ");
for(i=0;i<n;i++)
printf(" %d",b[i]);
getch();
return 0;
}
#include<stdio.h>
int display(int a[],int n)
{
int i;
for(i=0;i<n;i++)
printf("\n%d",a[i]);
return 0;
}
int shift(int a[],int n)
{
int i;
for(i=n;i>0;i--)
a[i]=a[i-1];
a[0]=1;
return n+1;
}
int main()
{
int i,a[10],n;
printf("enter the number of elements to be inserted in the array:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=n-1;i>=-1;i--)
{
if(i<0){
n=shift(a,n);break;}
if(((a[i]+1)/10)<1){
a[i]=a[i]+1;
break;}
else
a[i]=(a[i]+1)%10;
}
display(a,n);
return 0;
}
use dynamic array(malloc and free)
- aravind mano May 14, 2013if the array size does not increase then
1. do inplace addition of the values from array[last_element]
if the array size increases then
1.copy the array values in temp variable {9,9,9}---> temp = 999.
2.free the array
3.dynamically allocate the array with size length+1
4.add 1 to 999 and copy it to the new array as {1,0,0,0}