Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
determine if 1 int is multiple of another - series of pairs Posted by raeiko on 6 Feb 2009 at 11:52 AM
Hello guys,

i have just compiled a C program that enters 2 numbers and established if the second one is multiple of the fiirst one.

I have 2 doubts:

1) the exercise ask to write a "function multiple that determines for a pair of integers whether the second integer is a multiple of the first".

2) i have to test it in a "program that inputs a series of pairs of integers"

I think that what i have done doesn't answer these 2 requirements.

Could you please have a look and correct my code?

Thanks a lot,

raeiko

#include <stdio.h>

int main( void ) /* function main begins program execution */
{
	int num1, num2, counter; /* declare variables */

	printf("Enter the first number:"); /* prompt for input */
	scanf("%d", &num1 ); /* read number from user */

	printf("Enter the secon number:"); /* prompt for input */
	scanf("%d", &num2 ); /* read number from user */

	for ( counter = 1; counter >= 2; counter++ );{
		if (num2 % num1 == 0)
			printf( "num2 is multiple of num1\n" );/* tests if the first number is a multiple of the second number */
		else {
			printf( "num2 is not multiple of num1\n" );
			}
		}

return 0; 
}

Report
Re: determine if 1 int is multiple of another - series of pairs Posted by Actor on 7 Feb 2009 at 2:33 AM
Only one comment. This program takes num1 == 0as a trigger to terminate the program. This seems to be a good choice since if you proceed with the program when num1 == 0 you will get a "divide by zero" error.

#include <stdio.h>

#define bool int
#define TRUE 1
#define FALSE 0

int main(void)    /* function main begins program execution */
{
    int num1, num2; /* declare variables */

    while (TRUE) {
        printf("\nEnter the first number:"); /* prompt for input */
	scanf("%d", &num1 ); /* read number from user */
		
	if (num1 == 0) /* quit program if first number is zero */
	    break ;

	printf("Enter the secon number:"); /* prompt for input */
	scanf("%d", &num2 ); /* read number from user */

	if (ismult(num1, num2))
	    printf( "num2 is multiple of num1\n" ) ;
	else
	    printf( "num2 is not multiple of num1\n" ) ;
    } 
	
    return 0; 
}

/* test if the first number is a multiple of the second number */
bool ismult(int num1, int num2)  
{
	return (num2 % num1 == 0) ;
}

Report
Re: determine if 1 int is multiple of another - series of pairs Posted by raeiko on 7 Feb 2009 at 6:00 AM
Hello,

thank you very much for your help.

Just a quick question: i have copied your code to VisualStudio Express 2005 but i get the following message from the compiler (but the program works if i start it without debugging):

1>------ Build started: Project: mcastriota_assignment4_Q1, Configuration: Debug Win32 ------
1>Compiling...
1>mcastriota_assignment4_Q1.cpp
1>c:\users\saphira\documents\visual studio 2005\projects\mcastriota_assignment4_q1\mcastriota_assignment4_q1\mcastriota_assignment4_q1.cpp(29) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files\microsoft visual studio 8\vc\include\stdio.h(295) : see declaration of 'scanf'
1>c:\users\saphira\documents\visual studio 2005\projects\mcastriota_assignment4_q1\mcastriota_assignment4_q1\mcastriota_assignment4_q1.cpp(35) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files\microsoft visual studio 8\vc\include\stdio.h(295) : see declaration of 'scanf'
1>c:\users\saphira\documents\visual studio 2005\projects\mcastriota_assignment4_q1\mcastriota_assignment4_q1\mcastriota_assignment4_q1.cpp(37) : error C3861: 'ismult': identifier not found
1>c:\users\saphira\documents\visual studio 2005\projects\mcastriota_assignment4_q1\mcastriota_assignment4_q1\mcastriota_assignment4_q1.cpp(48) : error C2365: 'ismult' : redefinition; previous definition was 'formerly unknown identifier'
1>Build log was saved at "file://c:\Users\Saphira\Documents\Visual Studio 2005\Projects\mcastriota_assignment4_Q1\mcastriota_assignment4_Q1\Debug\BuildLog.htm"
1>mcastriota_assignment4_Q1 - 2 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

is there something wrong?

Thanks again,

raeiko
Report
Re: determine if 1 int is multiple of another - series of pairs Posted by Actor on 7 Feb 2009 at 6:47 AM
: Hello,
:
: thank you very much for your help.
:
: Just a quick question: i have copied your code to VisualStudio
: Express 2005 but i get the following message from the compiler (but
: the program works if i start it without debugging):
:
: 1>------ Build started: Project: mcastriota_assignment4_Q1,
: Configuration: Debug Win32 ------
: 1>Compiling...
: 1>mcastriota_assignment4_Q1.cpp
: 1>c:\users\saphira\documents\visual studio
: 2005\projects\mcastriota_assignment4_q1\mcastriota_assignment4_q1\mca
: striota_assignment4_q1.cpp(29) : warning C4996: 'scanf': This
: function or variable may be unsafe. Consider using scanf_s instead.
: To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help
: for details.
: 1> c:\program files\microsoft visual studio
: 8\vc\include\stdio.h(295) : see declaration of 'scanf'
: 1>c:\users\saphira\documents\visual studio
: 2005\projects\mcastriota_assignment4_q1\mcastriota_assignment4_q1\mca
: striota_assignment4_q1.cpp(35) : warning C4996: 'scanf': This
: function or variable may be unsafe. Consider using scanf_s instead.
: To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help
: for details.
: 1> c:\program files\microsoft visual studio
: 8\vc\include\stdio.h(295) : see declaration of 'scanf'
: 1>c:\users\saphira\documents\visual studio
: 2005\projects\mcastriota_assignment4_q1\mcastriota_assignment4_q1\mca
: striota_assignment4_q1.cpp(37) : error C3861: 'ismult': identifier
: not found
: 1>c:\users\saphira\documents\visual studio
: 2005\projects\mcastriota_assignment4_q1\mcastriota_assignment4_q1\mca
: striota_assignment4_q1.cpp(48) : error C2365: 'ismult' :
: redefinition; previous definition was 'formerly unknown identifier'
: 1>Build log was saved at "file://c:\Users\Saphira\Documents\Visual
: Studio
: 2005\Projects\mcastriota_assignment4_Q1\mcastriota_assignment4_Q1\Deb
: ug\BuildLog.htm"
: 1>mcastriota_assignment4_Q1 - 2 error(s), 2 warning(s)
: ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
: ==========
:
: is there something wrong?
:
: Thanks again,
:
: raeiko
:
The program compiles and runs with my antique C compiler. My guess is that you are compiling it with a C++ compiler since it doesn't like scanf. I really don't know that much about C++ but you could try the following:

1. See if there is some way to tell the compiler that the code is C, not C++.

2. Try adding #define scanf scanf_s to the list of #defines.

3. Try putting all the code for ismult(int num1, num2) before main(), or

4. Prototype ismult(int num1, num2)




Report
Re: determine if 1 int is multiple of another - series of pairs Posted by raeiko on 8 Feb 2009 at 7:04 AM
Hello again,

on the basis of your hints, i have revised my code. I cannot use the boolean functions because i haven't studied them yet...

Here is what i have done so far, but there are some errors in the second part because, to be honest, after reading and reading again from my book how to call a fuction, i still haven't it clear
The program only return the result of the last couple of numbers.

I'm sure i'm making some stupid mistake...

/* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */

#include <stdio.h>
#include <math.h>

int multiple ( int j, int z); /* function prototype */

int main( void )    /* function main begins program execution */
{
    int num1, num2, x, y; /* declare variables */
	
	for ( x = 1; x <= 5; x++ ){	
		printf( "Enter the first number:" ); /* prompt for input */
		scanf_s("%d", &num1 ); /* read number from user */

		if ( num1 == 0){
			break;
			printf( "\nBroke from loop because num1 must be greater than 0\n" ); /* break loop if num1 == 0 */
			} 
		else {
			printf( "Enter the second number:" ); /* prompt for input */
			scanf_s( "%d", &num2 ); /* read number from user */
			}
		}

	for ( y = 1; y <= x ; y++ )
		if( multiple == 0){
		printf( "%d is multiple of %d\n", num2, num1);
		}
	else {
		printf( "%d is not multiple of %d\n", num2, num1 );
		}

return 0;
}

int multiple ( int j, int z ) /* copy of the argment to function */
{
	return z % j;
} /* end of multiple function */



Report
Re: determine if 1 int is multiple of another - series of pairs Posted by Actor on 8 Feb 2009 at 8:48 AM
: The program only return the result of the last couple of numbers.

Because you have two loops. The first reads input, the second writes output. But every iteration of the first loop overwrites the data from the previious iteration, meaning the only data the second loop sees is the data from the last iteration of the first loop. You need one loop that has both input and output.

	for ( x = 1; x <= 5; x++ ){	
		printf( "Enter the first number:" ); /* prompt for input */
		scanf_s("%d", &num1 ); /* read number from user */

		if ( num1 == 0){
			break;  { the printf is unreachable because it is
                                preceded by the break.
                                Reverse the order of these two statements }
			printf( "\nBroke from loop because num1 must be greater than 0\n" ); /* break loop if num1 == 0 */
			} 
		else {
			printf( "Enter the second number:" ); /* prompt for input */
			scanf_s( "%d", &num2 ); /* read number from user */
			}
		}

Report
Re: determine if 1 int is multiple of another - series of pairs Posted by raeiko on 8 Feb 2009 at 9:01 AM
Thanks a lot for your precious help!


raeiko




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.