Urgent Help for homework - sum and difference Urgent Help for homework - sum and difference

I am not good at programming, my teacher gave me to write a program and i have to finish it in 2 days. Here is the program which i have to write


Write a program to determine the sum Z=X+Y and the difference Z=X-Y, where X, Y are n-digit integer decimal numbers and n<=100.


When I asked how to do that he explained me and he helped me little. Here is what he wrote. Ofcourse they are only clues about the way.


Z=X+Y
cost nmax=100
int X[nmax]
for(int i=0; i<nmax; i++)
x[i]= rand()%10;
z[i] = X[i]+ Y[i] + c;


Can anyone help me about that program?

Thanks
addition:
for nth digit in the array, starting from the least significant one,
z[n] = x[n]+y[n];
since z[n] is a digit, if z[n] turned out to be >= 10, subtract 10 from it and add 1 to z[n+1].

subtraction is the same thing, except that you check if x[n]-y[n] < 0 and then add 10 and subtract 1.
That's a pretty good start.

He's just saying that you should use an array of ints to hold each individual digit.

For example, to hold the value 2468, you'd have an array:

int num[4] = { 2, 4, 6, 8 };

To be able to add and subtract two such "numbers" you just have to think about how you would add and subtract two numbers with pencil and paper. Then go write the program to do it the same way.


UGLY WORKING CODE, at least for adding, you can use carry as overflow detection.
Substracting works only if X is bigger than Y.
If Z can have more digit than X and Y, you can avoid overflow.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define nmax 100
int x[nmax]={0};
int y[nmax]={0};
int z[nmax]={0};
int carry;

int main(){
  int x[nmax]={0};
  int y[nmax]={0};
  int z[nmax]={0};
  int carry;
  int i;

  srand ( time(NULL) );

  //Initializing x[] and y[];
  for(i=0; i<nmax; i++){
    x[i]= rand()%10;
    y[i]= rand()%10;
  }
 
  //Adding
  carry=0;
  for(i=0; i<nmax; i++){
    carry=carry+x[i]+y[i];
    z[i]=carry%10;
    carry=carry/10;
  }

  //Formatted printing of 77 digit (console is too small)
  printf("  ");
  for(i=76; i>-1; i--)
    printf("%d",x[i]);
  printf("\n+ ");
  for(i=76; i>-1; i--)
    printf("%d",y[i]);
  printf("\n= ");
  for(i=76; i>-1; i--)
    printf("%d",z[i]);
  printf("\n\n");

  //Substracting
  carry=11;
  for(i=0; i<nmax; i++){
    carry=-1+carry/10+x[i]-y[i]+10;
    z[i]=carry%10;
  }

  //Formatted printing of 77 digit this time for substracting
    printf("  ");
  for(i=76; i>-1; i--)
    printf("%d",x[i]);
  printf("\n- ");
  for(i=76; i>-1; i--)
    printf("%d",y[i]);
  printf("\n= ");
  for(i=76; i>-1; i--)
    printf("%d",z[i]);
  printf("\n");

}


Aaa 1 more thing, for substracting, if Y>X switch them and put - sign before result, that still need z[101] to fully work.
Last edited on
Hi,

It was really great help madredcake

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define nmax 100
int x[nmax]={0};
int y[nmax]={0};
int z[nmax]={0};
int carry;

int main(){
int x[nmax]={0};
int y[nmax]={0};
int z[nmax]={0};
int carry;
int i;

srand ( time(NULL) );

//Initializing x[] and y[];
for(i=0; i<nmax; i++){
x[i]= rand()%10;
y[i]= rand()%10;
}

//Adding
carry=0;
for(i=0; i<nmax; i++){
carry=carry+x[i]+y[i];
z[i]=carry%10;
carry=carry/10;
}

//Formatted printing of 77 digit (console is too small)
printf(" ");
for(i=76; i>-1; i--)
printf("%d",x[i]);
printf("\n+ ");
for(i=76; i>-1; i--)
printf("%d",y[i]);
printf("\n= ");
for(i=76; i>-1; i--)
printf("%d",z[i]);
printf("\n\n");

//Substracting
carry=11;
for(i=0; i<nmax; i++){
carry=-1+carry/10+x[i]-y[i]+10;
z[i]=carry%10;
}

//Formatted printing of 77 digit this time for substracting
printf(" ");
for(i=76; i>-1; i--)
printf("%d",x[i]);
printf("\n- ");
for(i=76; i>-1; i--)
printf("%d",y[i]);
printf("\n= ");
for(i=76; i>-1; i--)
printf("%d",z[i]);
printf("\n");

}


Could someone complate that program without errors?
what are the errors?
After 2 days i need gave my homework to my teacher and he will give me the mark. and i need o complate this problem in his way. He showed me how does it has to start but i need to complate it. an someone help me to finish my program, here is the what he wrote;

n= 100
int x(100), Y(100), Z(101)

for (i=0, i<n,i++)
z[i]=x[i]+y[i]+c;
if (z[i]>9)
{c=1; z[i]=z[i]%10}

who can create a C++ program from this content. and plus i need to write it for z=x-y too
Firstly, x(100) and others should be x[100].

This is pretty much complete addition. Just put it in a function.
For subtraction change x+y+c to x-y-c and z>9 to z < 0 and z = z%10 to ...
Well, that % is kind of unneeded here (or in addition).. If you add two valid digits, the sum will be below 20 and if you subtract it will be above -10. You only need to add or subtract a 10 here to move the digit to it's correct value.
could u write me a program?
no
im out of time now, i have to pass that exam
Topic archived. No new replies allowed.