Hi guys,
I'm a beginner in C and I need your help please. They gave us at school this small program to write. It is supposed to ask a user for two numbers and than make modulo division and multiply them like this: ImageShack® - Online Photo and Video Hosting (an example)
I did the modulo division but I am stack with the multiplication.
Any suggestions would be appreciated :]
11 replies to this topic
#1
Posted 03 December 2010 - 01:43 PM
|
|
|
#2
Posted 03 December 2010 - 01:49 PM
#3
Posted 03 December 2010 - 02:00 PM
Yeah. The exact wording of the task can be found here: http://img27.imagesh...50/13965087.gif
#4
Posted 03 December 2010 - 02:19 PM
Oh, I understand now. What is your code so far for the multiplication?
Latinamne loqueris?
#5
Posted 03 December 2010 - 02:23 PM
Basically I have nothing for the multiplication. I have no clue how to start. So far I wrote only the part with modulo division. It looks like this:
// Lab3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int division(int x, int y) {
if (y==0) {
return 'e';
}
if (y!=0) {
return (x-((x/y)*y));
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int x,y,result;
printf ("Enter number 1 (x)=\n");
scanf ("%d", &x);
printf("\n Enter number 2 (y)=\n");
scanf ("%d", &y);
result=division(x,y);
if (result=='e') {
printf ("\n\nERROR: division by 0 is forbidden\n");
} else {
printf ("\n\nResult of modulo division is: %d\n", result);
}
return 0;
}
#6
Posted 03 December 2010 - 02:51 PM
Well, as for the actual calculations, you would probably want to load the number into an array, each of the cells containing one digit. For this purpose, you would probably want to use the unsigned char type. Then, you would just do the multiplications as you would do by hand, loading each number in to an array of int's, and then add up the numbers. The printing shouldn't be that hard.
Latinamne loqueris?
#7
Posted 03 December 2010 - 03:03 PM
The printing will be fine. However. How do I load a number into an array? What kind of array am I supposed to use, one or two dimensional?
#8
Posted 03 December 2010 - 03:08 PM
@ziom123,
e has decimal value of 101 (ASCII table) and if you'll pass such numbers that modulo is 101, your program will says it's division by zero error. Instead, you should probably return -1 in your division function. Or better yet, if you can code in C++, you can throw an exception and then catch it. Also, your function code could be a bit more optimized for reading:
e has decimal value of 101 (ASCII table) and if you'll pass such numbers that modulo is 101, your program will says it's division by zero error. Instead, you should probably return -1 in your division function. Or better yet, if you can code in C++, you can throw an exception and then catch it. Also, your function code could be a bit more optimized for reading:
if (y == 0) {
return -1;
} else { // since y is not 0 we don't need to check if it's different than 0
return x - ((x / y) * y);
}
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#9
Posted 03 December 2010 - 03:20 PM
You would take an integer value, take it modulo 10, and the output of that will be the leftmost digit. Then divide the number by 10, and repeat this process, until n/10=0
Latinamne loqueris?
#10
Posted 03 December 2010 - 03:32 PM
@Flying Dutchman
Thanks. I changed it to -1 instead of e. Now the modulo division works perfectly fine.
@mebob
That's an idea. I will try to write it down in C, compile and I will let you guys know tomorrow morning how it works.
Thanks for the help so far :)
Thanks. I changed it to -1 instead of e. Now the modulo division works perfectly fine.
@mebob
That's an idea. I will try to write it down in C, compile and I will let you guys know tomorrow morning how it works.
Thanks for the help so far :)
#11
Posted 04 December 2010 - 04:04 AM
Morning! I did as you told me to. Now the program calculates everything correctly, however doesn't display the calculations as required if numbers are greater than 99. Basically anything that is more than a 2 digit number messes up the process of displaying the results.
Maybe I am just gonna quit this gibberish explanation and show you the code:
Maybe I am just gonna quit this gibberish explanation and show you the code:
// Lab3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int division(int x, int y) {
if (y==0) {
return -1;
}
if (y!=0) {
return (x-((x/y)*y));
}
}
int multi(int x, int y) {
int a,b,c,d,e,f,result;
e=x;
f=y;
a=division(x,10);
b=division(y,10);
c=a*b;
result=division(c,10);
c/=10;
a=x/10;
a=(a*b)+c;
printf(" %d%d\n",a,result);
a=division(x,10);
y/=10;
d=y*a;
result=division(d,10);
d/=10;
a=x/10;
a=(a*y)+d;
printf(" %d%d\n",a,result);
printf("_________________\n");
printf(" %d\n", e*f);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
int x,y,result;
printf ("Enter number 1 (x)=\n");
scanf ("%d", &x);
printf("\n Enter number 2 (y)=\n");
scanf ("%d", &y);
result=division(x,y);
if (result==-1) {
printf ("\n\nERROR: division by 0 is forbidden\n");
} else {
printf ("\n\nResult of modulo division is: %d\n\n", result);
}
printf(" %d\n %d\n", x, y);
printf("_________________\n");
result=multi(x,y);
return 0;
}
#12
Posted 06 December 2010 - 12:36 PM
Well, from what I can tell, result only has the last digit of the number, and a only has the second. You are limiting yourself to only the lowest order 2 digits.
Latinamne loqueris?
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









