A lil processing with arrays

The aim of the program is to put a 9 digit number in the array, and use functions to increment the number
For ex if i enter 123456788 in the array
after the processing the output must be 123456789
Can someone please tell me how must I edit the code to make it work ?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<math.h>
#include<iostream.h>

long int c2n(long int tempar[9])   // this function converts the array into a number
{
	long int num=0;
	for(long int x=0;x<=8;x++)
	num+=(long)((tempar[8-x])*pow(10,x)
	return num;
}


// this fn converts array contents into a number, increments the number, and stores the incremented number back into the array
void cic(int x1,int x2,int x3,int x4,int x5,int x6,int x7,int x8,int x9)
{
	long int tempar[9]={x1,x2,x3,x4,x5,x6,x7,x8,x9};
	long int num=c2n(tempar);
	num++;
	cout<<"\n\nnew num\n\n"<<num;
	x9=num%10;
	num/=10;
	x8=num%10;
	num/=10;
	x7=num%10;
	num/=10;
	x6=num%10;
	num/=10;
	x5=num%10;
	num/=10;
	x4=num%10;
	num/=10;
	x3=num%10;
	num/=10;
	x2=num%10;
	num/=10;
	x1=num%10;
	num/=10;
	
}

void main()
{
long int x[9]={1,2,3,4,5,6,7,8,8};   // input
cic(x[0],x[1],x[2],x[3],x[4],x[5],x[6],x[7],x[8]);
cout<<endl<<endl;

for(long int i=0;i<=8;i++)   // output
cout<<x[i];

}


Last edited on
You're using strange logic here...

This is how I would do:
Ask the user for input. Store that input into a string. Then use a loop and the memberfunction .substr() to store the first 9 numbers in the string to your array. After that, use a recursive function that increases an element of the function, and if the element has become greater then 10, calls itself for the next element.

Line 20 till 37 can be done with a for loop and an array, wich would take about 3 lines or someting.
I am not able to really get what you are trying to tell,
Can you code it please ?
I'm not going to do your homework. Maybe this wil help you to store the userinput into an array:
http://www.cplusplus.com/reference/string/string/substr.html
http://www.cplusplus.com/reference/clibrary/cstdlib/strtol.html

I would use a global array, so you can access it from your whole code. For the function you will need a variable to keep track of wich element you are adding to, and another to see how much. It's up to you to figure the rest :)
I would use a global array
This did the needful
Btw, this isn't my homework, I am trying to make a sudoku solver & this was something related
Anyway, thanks a lot :)
Glad I could help :)
How is this useful for a sudoku solver, may I ask?
Topic archived. No new replies allowed.