ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
im trying to do a small program in C for the following question and would approeciate any help:
To round of an integer i to the next largest even multiple of another integer j, the following formula can be used:
Next_multiple = i + j - i % j
Write a program to find the next largest even multiple for the following values of i and j:
i:: j
365:: 7
12,258:: 23
996:: 4
(i and the corresponding j value)
this is the program code that I attempted
Code:
#include <stdio.h>
int main (void)
{
int i;
int j;
int nextmultiple;
nextmultiple = (i + j) - (i % j);
i = 365;
j = 7;
printf ("The next largest even multiple when i = 365 and j = 7 is %i\n", nextmultiple);
i = 12258;
j = 23;
printf ("The next largest even multiple when i = 12,258 and j = 23 %i\n", nextmultiple);
i = 996;
j = 4;
printf ("The next largest even multiple when i = 996 and j = 4 %i\n", nextmultiple);
return 0;
}
int nextmultiple;
nextmultiple = (i + j) - (i % j);
You probably want nextmultiple to be a function instead of an integer holding some value derived from garbage (uninitialized) integers i and j. The contents of nextmultiple are not going to be re-evaluated whenever the variable is used.
Hmmmm yes what taylor said is correct. In C you cannot define a varable this way.
more to do this as a function and run the function every time. If this is homework please investigate further why your program is wrong and not just use this as the answer as you will be more confused later
what you probably want to do is somehting like this
Code:
#include<stdio.h>
int findvalue(int,int);
int main()
{
int i = 0;
int j = 0;
i = 365;
j = 7;
printf("The next largest even multiple when i = 365 and j = 7 is %d",findvalue(i,j));
/* Do more like this */
return 0;
}
int findvalue(int x, int y)
{
int temp = 0;
temp = (x + y) - (x % y);
return temp;
}
Also if you havent gone over function calls and variable passing then your instructer may see this as cheating. He probably wants you to do it the long way.
is there a way to do it without function calls and variable passing?
i appreaciate any help.
Yes and no! -Actually you cannot write a C program without function calls (remember that even the call of 'main' is such one). But I guess this is not what you want know. You can of course re-calculate the value of "nextmultiple" each time you changed 'i' or 'j' (simply copy/paste the assignment of it under every change of 'i' or 'j'). I think this will accomplish what you want. But what you want is really no good style. Unless your teacher forbids the use of extra functions, you should really use them! -Btw. 'printf' is also a function.
ive written a small program to accept 2 integeres and then divide the first one by the second.
the code is:
Code:
int main (void)
{
int i, j;
float result;
printf ("Please enter two integers.\n");
scanf ("%i %i", &i, &j);
if ( j == 0 )
printf ("\nDivision by zero.\n");
else
result = (float) i / j;
printf ("\nThe result is %f\n", result);
return 0;
}
the only thing is that when i divide by zero it shows 'Division by zero' as well as the result, when it should only show the division by zero.
any suggestions or help would be appreciated.
could someone also tell me how to compile and run c programs in kubuntu? which program should i use?
If you're new to C programming, I recommend taking a look at some of the good tutorials scattered about the internet. Just do an internet search for "c programming tutorial"; here's one I found: http://vergil.chemistry.gatech.edu/r...orial/toc.html
taylor i'm already working from internet tutorials and questions.
and ive got another one, this one for a simple calculator thats supposed to work like a pocket calculator, ie. enter the number followed by the sign and then enter.
its supposed to start when the user enters the number followed by the 'S' character and then exit the program and print the results when the user enters 0 E. what its doing is looping on entering the first character when it shouldnt.
Code:
#include <stdio.h>
int main (void)
{
float number, result;
char sign;
printf ("Begin Calculations\n");
scanf ("%f %c", &number, &sign);
result = 0;
if ( sign != 'e' ) {
switch ( sign )
{
case 'S':
result = number;
printf ("\n%f", result);
break;
case '+':
result = result + number;
printf ("\n%f", result);
break;
case '-':
result = result - number;
printf ("\n%f", result);
break;
case '/':
if ( i == 0 )
printf ("\nDivision by zero.");
else
{
result = result / number;
printf ("\n%f", result);
}
break;
case '*':
result = result * number;
printf ("\n%f", result);
break;
default:
printf ("Unknown operator.\n");
break;
}
}
printf ("\nThe result of the calculations are %f", result);
printf ("\nEnd of calculations.\n");
return 0;
}
id appreciate any help from experienced programmers.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.