Welcome to Dream.In.Code
Become a C++ Expert!

Join 136,928 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,823 people online right now. Registration is fast and FREE... Join Now!




Can someone check this

 
Reply to this topicStart new topic

Can someone check this

njkiller07
31 Mar, 2008 - 06:28 PM
Post #1

New D.I.C Head
*

Joined: 24 Mar, 2008
Posts: 10

k my topic is

You must
Prompt the user for all input values.
Echo (output) each input value.
Label all output values.
Format your output for ease of comprehension.

An array can be used to store large integers one digit at a time. For example, the integer 1234 could be stored in the array a by setting a[0] to 1, a[1] to 2, a[2] to 3, and a[3] to 4. However, for this exercise you might find it more useful to store the digits backward, that is, place 4 in a[0], 3 in a[1], 2 in a[2], and 1 in a[3]. In this exercise you will write a program that reads in two positive integers that are 20 or fewer digits in length and then outputs the sum of the two numbers. Your program will read the digits as values of type char so the number is 1234 is read as the four characters '1', '2', '3', '4'. After they are read into the program, the characters are changed to values of type int. The digits will be read into a partially filled array, and you might find it useful to reverse the order of the elements in the array is up to you. It can be done either way, and each way has its advantages and disadvatages.) Your program will perform the addition by implementing the usual paper-and-pencil addition algorithm. The result of the addition is stored in an array of size 20 and the result is then written to the screen. If the result of the addition is an integer with more than maximum number of digits (that is, more than 20 digits), then your program should issue a message saying that it has encountered "integer overflow." You should be able to change the maximum length of the integers by changing only one globally defined constant. Include a loop that allows the user to continue to do more additions until the user says the program should end.


so i designed the code and i got to this


cpp

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


#define MAX_DIGITS 20


// Finds the max of two integers
int max(int a, int cool.gif {
if (a >= cool.gif
return a;
return b;
}

// Reads the number from the keyboard into array num
// Returns the number of digits read
int readNumber(int *num) {

int i = 0;
while (1) {
char c = getchar();
if (c == 'q' || c == 'Q')
exit(0);

if (c == '\n')
return i;

num[i] = (int) (c - 48); // Convert char to int
i++;

if (i >= MAX_DIGITS)
return i;
}
}

void printResult(int num[],int size) {

int i;
for (i = MAX_DIGITS - size; i < MAX_DIGITS; i++) {
printf("%d",num[i]);
}
printf("\n");
}

int main() {

while (1) {

int num1[MAX_DIGITS];
int num2[MAX_DIGITS];
int result[MAX_DIGITS];

int size1;
int size2;

printf("Please enter the first number (Q) to exit: ");
size1 = readNumber(num1);
printf("Please enter the second number (Q) to exit: ");
size2 = readNumber(num2);

int i1 = size1 - 1;
int i2 = size2 - 1;

// Initialize result array
int i;
for (i = 0; i < MAX_DIGITS; i++) {
result[i] = 0;
}

int lengthOfResult = 0;

int k;
for (k = MAX_DIGITS - 1; k >= 0; k--) {


result[k] += num1[i1] + num2[i2];

if (result[k] > 9) { // We need to carry
if (k == 0) {
printf("Error: Integer Overflow\n");
exit(1);
}
result[k] = result[k] - 10;
result[k-1]++;

if (k == MAX_DIGITS - max(size1,size2))
lengthOfResult++;
}

if (i1 > 0)
i1--;
else
num1[i1] = 0;
if (i2 > 0)
i2--;
else
num2[i2] = 0;

if (k > MAX_DIGITS - max(size1,size2) - 1)
lengthOfResult++;

}

printf("length: %d\n",lengthOfResult);
printResult(result,lengthOfResult);

}
}

*edit: Please use code tags in the future, thanks! code.gif



can someone pls chk this for me

thxx in advance

This post has been edited by Martyr2: 31 Mar, 2008 - 09:46 PM
User is offlineProfile CardPM
+Quote Post

pertheusual
RE: Can Someone Check This
31 Mar, 2008 - 08:46 PM
Post #2

D.I.C Head
**

Joined: 26 Jan, 2008
Posts: 231



Thanked: 3 times
My Contributions
Hey there.

The code doesn't look too bad. I didn't go over it 100%, but the one thing I noticed right away was that your readNumber method doesn't follow the same pattern as the rest of the code.

Most of your code starts at the right edge of the array(assuming left to right indexing), but in the readNumber method, i starts at zero. You'll have to read into a char array, then get the length, then put the info into an int array or just shift your current array over to the right edge instead of the left.

Also, that was really annoying to read, please put code inside [code] tags.

Per
User is offlineProfile CardPM
+Quote Post

njkiller07
RE: Can Someone Check This
1 Apr, 2008 - 04:59 AM
Post #3

New D.I.C Head
*

Joined: 24 Mar, 2008
Posts: 10

QUOTE(pertheusual @ 31 Mar, 2008 - 09:46 PM) *

Hey there.

The code doesn't look too bad. I didn't go over it 100%, but the one thing I noticed right away was that your readNumber method doesn't follow the same pattern as the rest of the code.

Most of your code starts at the right edge of the array(assuming left to right indexing), but in the readNumber method, i starts at zero. You'll have to read into a char array, then get the length, then put the info into an int array or just shift your current array over to the right edge instead of the left.

Also, that was really annoying to read, please put code inside [code] tags.

Per



sorry abt that

do u knw how i can fi x that??
can u pls show it to me?
User is offlineProfile CardPM
+Quote Post

pertheusual
RE: Can Someone Check This
1 Apr, 2008 - 05:29 AM
Post #4

D.I.C Head
**

Joined: 26 Jan, 2008
Posts: 231



Thanked: 3 times
My Contributions
Please remember that this is just to point you in the right direction, I didn't test it and it's alignments might be off by one.

cpp

if (c == '\n'){
int j;
for(j = 0; j < i; j++){
num[MAX_DIGITS-i+j] = num[j];
num[j] = 0;
}
return i;
}


If you have this in your read function, it will shift all of the numbers over after it hits the '\n'. It does so based on the size i of the ascii text entered.

Per
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 10:29PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month