LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-17-2006, 08:36 AM   #1
wongfoo
LQ Newbie
 
Registered: Mar 2006
Distribution: Ubuntu 5.10
Posts: 15

Rep: Reputation: 0
Arrow Integer programming question in C


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;
}
ps. this was compiled in Windows

Last edited by wongfoo; 06-17-2006 at 08:38 AM.
 
Old 06-17-2006, 08:56 AM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Quote:
Originally Posted by wongfoo
Code:
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.
 
Old 06-17-2006, 11:47 AM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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.

Last edited by exvor; 06-17-2006 at 11:53 AM.
 
Old 06-22-2006, 08:15 AM   #4
wongfoo
LQ Newbie
 
Registered: Mar 2006
Distribution: Ubuntu 5.10
Posts: 15

Original Poster
Rep: Reputation: 0
is there a way to do it without function calls and variable passing?

i appreaciate any help.
 
Old 06-22-2006, 08:32 AM   #5
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
Quote:
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.
 
Old 06-22-2006, 08:58 AM   #6
wongfoo
LQ Newbie
 
Registered: Mar 2006
Distribution: Ubuntu 5.10
Posts: 15

Original Poster
Rep: Reputation: 0
thanks Flesym
 
Old 06-23-2006, 09:56 AM   #7
wongfoo
LQ Newbie
 
Registered: Mar 2006
Distribution: Ubuntu 5.10
Posts: 15

Original Poster
Rep: Reputation: 0
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?

thanks
 
Old 06-23-2006, 10:06 AM   #8
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
without brackets your else will only apply to one line.
Code:
else
{
       result = (float) i / j;
       printf ("\nThe result is %f\n", result);
}
 
Old 06-23-2006, 10:14 AM   #9
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
And the answer to your other question

Quote:
Originally Posted by wongfoo
could someone also tell me how to compile and run c programs in kubuntu? which program should i use?
Like on any other Linux system you can use gcc for compilation. If it isn't installed already, do this with:
Code:
sudo apt-get install gcc

Last edited by Flesym; 06-23-2006 at 10:19 AM.
 
Old 06-23-2006, 10:22 AM   #10
wongfoo
LQ Newbie
 
Registered: Mar 2006
Distribution: Ubuntu 5.10
Posts: 15

Original Poster
Rep: Reputation: 0
thanks for your help xhi and Flesym

how would I compile and output the file from the command line?
 
Old 06-23-2006, 10:27 AM   #11
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
heh. i didnt read the whole way through the first time.

try `man gcc` to see all the options.

it can be as simple as

gcc filename.c

and your output will be named a.out.. (or you can name the output with the -o option)

then run it from the current dir with ./a.out
 
Old 06-23-2006, 11:24 AM   #12
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
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
 
Old 06-24-2006, 10:55 AM   #13
wongfoo
LQ Newbie
 
Registered: Mar 2006
Distribution: Ubuntu 5.10
Posts: 15

Original Poster
Rep: Reputation: 0
thanks for your help people.

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.

thanks.

also how do you do a 'clear screen' in C ?
 
Old 06-24-2006, 01:09 PM   #14
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
i dont understand what you are saying the problem is.

>what its doing is looping on entering the first character when it shouldnt.
i dont see a loop here
 
Old 06-24-2006, 01:54 PM   #15
dinojerm
Member
 
Registered: Apr 2004
Location: NJ,US
Distribution: Debian Sid
Posts: 33

Rep: Reputation: 15
You probably want to enclose most of that code in a loop, perhaps something like this:
Code:
do {
scanf ("%f %c", &number, &sign);
//Calculation code
} 
while(sign!='e');
printf ("\nThe result of the calculations are %f", result);
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash integer handling question mikee78 Programming 11 11-14-2005 04:53 AM
Help!!! Easy Question Help!! Integer Divison Mistro116@yahoo.com Programming 6 10-22-2005 11:39 AM
MySQL data type question: timestamp versus integer to hold time vharishankar Programming 4 07-07-2005 09:01 PM
C programming assigning an integer value to a string Linh Programming 4 06-22-2003 07:02 AM
C programming error. warning: comparison between pointer and integer Linh Programming 4 06-06-2003 03:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration