0
Here’s the thing, I do an info tech, and I’ve been learning C for just two months, and I don’t know much yet. Recently my teacher asked me to make a program in which user entered with two numbers one, and each position of two vectors n, were filled down with each digit of that number. Ex: The user has typed 284, the vector (of 100 positions) will have pos 100 = 0, a 99 = 0, (...) a 3 = 2, a 2 = 8 and a 1 = 4. And from there, we need to simulate an addition, a subtraction a division and a multiplication, working only with these vector boxes.
The output of the addition part would be more or less like this:
1
[0][0][2][5][4]
+
[0][0][1][9][4]
=
[0][0][4][4][8]
Very complicated knowing that you can only work with vector houses right?!
That’s the problem, hence I’m not able to proceed because of the title error, see my code:
#include <stdio.h>
int n[10], n2[10], num, nun, i=9, j=9, sv=0, on, res[4][20], opc[4][20];
main(){
printf("Insert the 1st number: ");
scanf("%d", &num);
sv = num;
while (num != 0)
{
n[i] = num%10;
num = num/10;
i--;
}
num = sv;
printf("Insert the 2nd number: ");
scanf("%d", &nun);
sv = nun;
while (nun != 0)
{
n2[j] = nun%10;
nun = nun/10;
j--;
}
nun = sv;
printf("Select an operation (1=addition; 2=subtraction; 3=multiplication; 4=division): ");
scanf("%d", &on);
while((on>4)||(on<0)){
printf("Try again: ");
scanf("%d", &on);
}
for (j=0; j<4; j++){
for (i=19; i>=0; i--){
opc[j][i]=0;
}
}
if (on==1)
{
for (i=10; i>=1; i++){
if ((n[i] + n2[i] + opc[1][10+i]) <= 9)
{
opc[1][10+i] += (n[i] + n2[i]);
}
if ((n[i] + n2[i] + opc[1][10+i]) > 9)
{
opc[1][10+i] += (n[i] + n2[i])%10;
opc[1][9+i] += (n[i] + n2[i])/10;
}
}
for (i=0; i<=9; i++){
printf("|%d|", n[i]);
}
printf("\n+\n");
for (i=0; i<=9; i++){
printf("|%d|", n2[i]);
}
printf("\n=\n");
for (i=19; i>=0; i--){
printf("|%d|", opc[1][i]);
}
}
else
{
printf("Coming soon :)");
}
}
If anyone can help me solve this mistake, or even suggest how I can do the rest of the program, I would be very grateful, I’m already a week working on it, doing the other exercises, but this one is extremely difficult.
A vector of
100
elements has no position100
, 'cause it goes from0
to99
. The number would be kept from the house0
forward ?– Isac
In case it is decreasing msm
– GE28