 |
 |
C Programmers NEEDED desperately!! Will pay MONEY!!!
|
 |
|
 |
|
Senior User
Join Date: Feb 2000
Location: Boston, MA
Status:
Offline
|
|
Hi guys,
I need your help desperately. I'm looking for a few C programmers to help me out to write a quick couple of programs. I stink at C and will be in eternal debt to you for any program you can help me out with. For a C-expert, these problems shouldn't take more than 5 mins each. I have 50 problems I need coded as soon as possible.
WILL YOU HELP?
I will pay you $5 for each program you can help me with. I am not lying that I will pay you $5. For example, if you can help me write say 10 of the programs, send me just one and I'll send you a money order for $5. When you get the money in the mail and see I'm not lying, you can send me the rest of the problems and I'll send you the rest of the money.
PLEEEASE HELP ME!! You can make good money fast!!! I will post the problems shortly, for now I just want to see who knows C and is willing to help. Can you? Will you?
[This message has been edited by Anthony the PC lover (edited 10-13-2000).]
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2000
Location: Boston, MA
Status:
Offline
|
|
Ok here are the first 13 problems: Start coding and I'll send you money!!!!
4.) Write a multiple file program that will read in a positive integer, say n, and then it prints out the sum of the first n positive integers. This program will contain three files: main.c , sum.h, and sum.c.
5.) Write a multiple file program that will read in a positive integer, say n, from a file. The name of the file is problem5.dat. The program will then print out the sum of the first n positive integers.
6.) Write a one-file one-function program which will read in a positive integer, say n, from the keyboard and then compute both the sum of all odd numbers between 1 and n; and the sum of all even numbers between 1 and n.
7.) Given a number, say n, the products of the first n positive integers (1..n) is called the factorial of the number. Mathematicians have also defined the factorial of the number zero to be the value one. Using the standard multi-file organization, write a program that inputs a non-integer value from the keyboard and outputs the factorial of the number. The solution should declare n to be an unsigned integer.
11.) Write a multi-file program that will repeatedly prompt the user to enter a single positive integer. After each positive integer is entered the user gets a message displayed stating whether the number is prime or not. Recall that: the number 1 is not a prime number, the number 2 is a prime number, and all the other prime numbers have only two factors (1 and themselves). The program will terminate when the user types in the EOF from the keyboard.
13.) Multiplying the digits of an integer and continuing the process gives the surprising result that the sequence of products always arrives at a single digit number. For example:
INPUT: 715….35….15….5
INPUT: 27….14….4
INPUT: 4000….0
The number of products necessary to reach a single digit number is called the persistence number of that integer. Thus the persistence number of 715 is 3, the persistence number of 27 is 2 and the persistence number of 4000 is 1.
Write a program to determine the persistence number for any positive integer. The integer will have at most 4 digits. Print out the integer, the products, and the persistence number. The format of the output is not overly important, but it should be readable. The persistence number must be labeled in some fashion.
Your program should use a data file names problem13.dat. The first line in the data file will indicate how many integers are to be processed. Call this number N (the maximum value for N is 20). Following the first line in the data file, there will be N lines in the data file with one integer per line.
3
715
27
2000
Sample OUTPUT:
715 35 15 5 Persistence number = 3
27 14 4 Persistence number = 2
4000 0 Persistence number = 1
[This message has been edited by Anthony the PC lover (edited 10-17-2000).]
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Jun 2000
Location: USA
Status:
Offline
|
|
I don't know C but I am curious as to the nature of this project. What kind of project is it?
|
|
09.11.01 - UNITED WE STAND
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Apr 2000
Location: Athens, Greece
Status:
Offline
|
|
Are you trying to pass a C course on your university?
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Dec 1999
Location: park city, ut usa
Status:
Offline
|
|
If you flip through the pages of your text book and stop at every example program, you should be able to find at least half of those. Sometimes the exact program, sometimes the whole program plus more stuff, and sometimes close enough where it's easy to modify.
If you don't at least try to write them, you'll fail your tests miserably.
What is the name of the text book btw?
|
Ok posters time to let them have it. MacNN Staff It's our duty to let MacNN know we hate intelliTXT!
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Jan 2000
Status:
Offline
|
|
Those are too easy. I have some hard ones to solve.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Dec 1999
Location: Tampa, Florida
Status:
Offline
|
|
4.
#include<sum.h>
#include<iostream.h>
main{
 for int i=1 to 1000{
 cout<<"I am a cheater";
 cout<<"I need to do my own homework";
 }
 system.kill;
}
[This message has been edited by The Godfather (edited 10-07-2000).]
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Jun 2000
Location: USA
Status:
Offline
|
|
Hey guys, Anthony may have a legitimate project that he is working on that is not school related. Don't jump to conclusions...then again he is a peecee lover. They really don't know what's going on. 
|
|
09.11.01 - UNITED WE STAND
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2000
Location: Boston, MA
Status:
Offline
|
|
Hello guys:
I am not working on a "project" but rather I'm trying to learn C programming and I'm completely lost. I really need assitance desperately so I want the answers to these problems so i can compare what I have to them, and learn what I'm doing wrong. If anyone can help me, I'd be undoubtedly the happiest guy on Earth.
PLEASE! I know this is a Mac forum but I know you guys are the best. I'm even willing to pay you money! I'm not trying to cheat...as a matter of fact I have 60 problems I'm working on and I only wanted answers to the first few.
If you know C, PLEASE help PLEASE!! I'll do anything!
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Jun 2000
Location: South Hadley, MA, USA
Status:
Offline
|
|
Here's some help:
main (void)
{
printf ("hello world!\n");
}
Figure out how to build and run it.
You've got to admit that it does look an awful lot like you're trying to cheat at a course. If you were simply trying to figure out what you're doing wrong, you'd ask specific questions.
[This message has been edited by denim (edited 10-10-2000).]
|
|
Is this a good place for an argument?
Peace on Earth, Good Will Toward Me
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Jan 2000
Status:
Offline
|
|
IF I have time I'll pick out a harder one and work on it.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Jan 2000
Status:
Offline
|
|
That was easy.
Code:
/* 10.) Write a one-function one file program which will continually
prompt the user to enter an integer value and then the program will
print out the sum of the individual digits of the number entered. The
program will loop until the user enters EOF from the keyboard. For
example, if the number 128 is entered, the answer is 11. (1+2+8) */
#include <stdio.h>
main(argc, argv)
int argc;
char** argv;
{
int n, temp, sum;
while(scanf("%d", &n)!=EOF){
sum=0;
while(n!=0){
temp=n/10.0; /* Dangerous type conversion here. int/float =? (round, chop?)*/
sum+=(n-10*temp);
if(0)fprintf(stderr, "n=%d temp=%d sum=%d\n", n, temp, sum);
n=temp;
}
printf("%d\n", sum);
}
}
[This message has been edited by Scott_H (edited 10-10-2000).]
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2000
Location: Boston, MA
Status:
Offline
|
|
5 bucks to Scott!!! Thank you!!!
I KNOW how to write the 'Hello World' program. I am not a moron, I have some knowledge of C. I know the functions/commands/values and stuff, I just never know when to use what. I am desperate for the kind of advanced help that great men like Scott can provide. God Bless your soul Scott!! Gimme your address and I'll send a money order ASAP. WOOHOO Thanks so much for your help guys(but mostly that ingenious ScottH!!!!!
If anyone else can help, or if people who have helped me can help me more, BELIEVE ME you will be REWARDED!!!!! This is no joke!
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Jan 2000
Status:
Offline
|
|
Oh Stop
I thought the above one was more complicated than it turned out to be. At first I was thinking I had to pop the digets off from the left. Taking from the right is the way to do it.
BTW I don't need the money.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2000
Location: Boston, MA
Status:
Offline
|
|
Scott, your code works perfectly and ingeniously, but it baffles the hell out of me. Where exactly in the program are the inputted digits added together?
PS. You are #1 in my book!
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Jun 2000
Location: South Hadley, MA, USA
Status:
Offline
|
|
|
|
|
Is this a good place for an argument?
Peace on Earth, Good Will Toward Me
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2000
Location: Boston, MA
Status:
Offline
|
|
Ok, ScottH, I figured what you did. You used division and multiplication math to split the numbers up individually, rather than a C command like 'getchar'. Amazing! I love it! You are quite a brain...this code is wonderful!
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Jan 2000
Status:
Offline
|
|
Change the if(0) to if(1) and you can get a print out while it loops.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2000
Location: Boston, MA
Status:
Offline
|
|
Hey can someone help me out with this multi-file program? It's a relatively easy program, it just prints the product of two inputted numbers from the keyboard. However, I can't seem to put the finishing touches on it. How do I make the files communicate with each other? Either the product.c or the product.h is all wrong, i know. Please help!!!
::::::::::::::
main.c
::::::::::::::
/* This is a multiple-file program which will read two positive integers from the keyboard and print out their product. This program contains three files: main.c, product.h, and product.c. */
#include <stdio.h>
#include "product.h"
#include "product.c"
int main()
{
int x,y,p;
printf("Enter a number\n");
scanf("%d",&x);
printf("Enter another number\n");
scanf("%d",&y);
p = product(x,y);
printf("The product of those numbers is %d\n",p);
return 0;
}
::::::::::::::
product.c
::::::::::::::
/* This is product.c, which is part three of Problem #3 */
extern product(int a, int b);
{
return (a * b);
}
::::::::::::::
product.h
::::::::::::::
/* This is product.h, which is part two of Problem #3 */
extern product(int a, int b);
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Jan 2000
Status:
Offline
|
|
Hi,
1) You don't include product.c. product.h needs to have a function prototype of the function calls in product.c
2) In product.c I don't know what "extern product...." is doing. Since a*b is of type int then that function needs to of that return type. So
"int product(..."
3) You need to compile product.c into an object file. On UNIX c compilers you need to use the -c flag to do that. Then when you compile main.c you need to link product.o into it with something like -l product.o {Can't remember. I've beed using Makefiles for so long  }
4) I think, depending on your compiler, you don't put the a and b in the product.h file. It just needs to know what type it's being passed, not the name that you used.
I'm not at "work" so I can't test the above.
[This message has been edited by Scott_H (edited 10-11-2000).]
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Jan 2000
Status:
Offline
|
|
Code:
/*product.h*/
/* This is product.h, which is part two of Problem #3 */
int product(int, int);
Code:
/*product.c*/
/* This is product.c, which is part three of Problem #3 */
int product(int a, int b)
{
return (a * b);
}
Code:
/*main.c*/
/* This is a multiple-file program which will read two positive integers from the keyboard and print out their product. This program contains
three files: main.c, product.h, and product.c. */
#include <stdio.h>
#include "product.h"
int main()
{
int x,y,p;
printf("Enter a number\n");
scanf("%d",&x);
printf("Enter another number\n");
scanf("%d",&y);
p = product(x,y);
printf("The product of those numbers is %d\n",p);
/*printf("The product of those numbers is %d\n",product(x,y));*/
return 0;
}
Code:
to compile
>cc -c product.c
>cc main.c product.o -o product
>./product
{Check my code for typos, I'm having a hard time typing today?}
[This message has been edited by Scott_H (edited 10-12-2000).]
[This message has been edited by Scott_H (edited 10-12-2000).]
[This message has been edited by Scott_H (edited 10-12-2000).]
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2000
Location: Boston, MA
Status:
Offline
|
|
Absolutely amazing! It works without a single error...not a typo in sight! You are a King among men, my friend. I see now how *.h files are headers and how they work. I love it! My only question is about this OBJ (or *.o) file...I'm using UNIX and I didnt think I needed this type of file, but it seems like I do when I make these multi-file programs. Do I need it? Can I delete it when I'm done? What exactly is it anyway?
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Jan 2000
Status:
Offline
|
|
The .o file is the compiled version of the .c file. After you compile main you can delete the .o because it's now a part of the compiled binary.
The idea is that you can build libraries of code to use day to day. So you need to compile the product.c just once then use it many times. No need to recompile it each time.If you edit product.c then you need to recompile it. That's where the makefile comes in. Using make and the makefile, make can keep track of what is current and what needs to be recompiled.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Jan 2000
Status:
Offline
|
|
Read the man page on cc (or gcc) so see what the different options to. Like for "-c" we get
Creates a relocatable object file for each named source file....
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2000
Location: Boston, MA
Status:
Offline
|
|
Ahh I see. Thats what I thought, that I could delete it once I'm done. It has no further purpose after compiling.
Anyhow, guess what? Another problem! Hoo Hoo! I am trying to make a program in which the user will enter two additional number arguments after the program name at the command line. These two numbers will create box of asterisks that has length and width of those two numbers entered at that command line.
If the user runs the program with 2 arguments, for example:
# program 4 6
The following will be printed on the screen:
******
******
******
******
----------------------------------------------------
Here is what I have so far. I cant seem to understand how to make the loops nested. I want to make it so if the first 'for' loop is true, the second loop inside it will also loop every time the first one loops; thus making the box. Please help my code o' crap:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char*argv[])
{
int x,a,b;
a=atoi(argv[1]);
b=atoi(argv[2]);
for (x=0; x<b; x++)
{
for (x=0; x<a; x++)
printf("*");
}
}
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Jan 2000
Status:
Offline
|
|
You can't use "x" again in the inner loop.
So here's what's happening. The first loop, on the first pass, sets x to zero. The second loop sets x to zero then loops it up to a. Then you break out to the first loop but adds one to it via the x++ in the first loop. If x (now set to a+1) is <b then it keeps going. Goes to the second loop and sets x to zero.....
Also you'll need to print a new line after the second loop before you end the first one.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2000
Location: Boston, MA
Status:
Offline
|
|
Hmmm interesting...so if I change (rename) one of the "x" loops to a "y" loop, for example, it will work? Or should I not be using two 'for' loops?
And good eye spotting the missing newline character! I caught that one myself a little while after posting that code in this thread. Thanks!
YOU ROCK!
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Jan 2000
Status:
Offline
|
|
Yes, two loops. Use something else for the second one, "y" is fine.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2000
Location: Boston, MA
Status:
Offline
|
|
|
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2000
Location: Boston, MA
Status:
Offline
|
|
Oh crap! It seems like I wrote the wrong program! This asterisk box program isnt supposed to read the two numbers from the command line, but rather the user is supposed to be prompted for a pair of integer numbers and then the program MUST LOOP continuously (keeps prompting for a pair of numbers and printing respective boxes) until the user types in the pair 0 0. If he types this, the program ends. Please help me out here. I know this is a really easy one to fix, I can tell just by looking at it. But for some reason, I just cant get it to work (im stupid, you see). Can you please help me out with this?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,x,y,l,w;
printf("Please enter a series of positive integer pairs\n");
scanf("%s %s",&l,&w);
a=atoi(l);
b=atoi(w);
for (x=0; x<b; x++)
{
printf("\n");
for (y=0; y<a; y++)
printf("*");
}
printf("\n");
}
[This message has been edited by Anthony the PC lover (edited 10-13-2000).]
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Jan 2000
Status:
Offline
|
|
In your scanf scan for integers "%d" and then don't bother with the atoi. You also have to put the scanf inside a loop. Maybe something like this (off the top of my head);
Code:
a=1;b=1; /*some dummy values to get it started;
while( (a!=0 && b!=0) | | scanf("%d %d\n", &a &b)!=EOF){
print stuff
}
Mayeb this is better
Code:
while(scanf("%d %d\n", &a &b)!=EOF){
if(a==0 && b==0)exit(0);
/*print stuff*/
}
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2000
Location: Boston, MA
Status:
Offline
|
|
Ok, I successfully wrote the asterisk box program. It looks like this:
/* This is a one-function one-file program which will read in a pair of
positive integers and print a solid box of asterisks that is the length
and width of the two integers entered. The program will loop until the
user enters the pair 0 0. */
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x,y,l,w;
printf("Please enter a series of positive integer pairs: ");
while (scanf("%d %d",&l,&w)!=EOF){
if (l==0 && w==0) exit(0);
for (x=0; x<w; x++)
{
printf("\n");
for (y=0; y<l; y++)
printf("*");
}
printf("\n\nPlease enter another pair: ");
}
}
Excellent! This is great!! Now, what I want to do next is modify this baby so that when the user enters the pair of integers, the box is printed out but is HOLLOW on the inside. For example, if the user enters the pair 6 4, the following will be printed to the screen (without the word "blank" hehehe):
******
*blank*
*blank*
******
How would I go about this? I have a few ideas but none of them are working correctly. Any suggestions would be incredibly appreciated.
PS. I would like to send a bottle of my finest wine to your house! Your help is teaching me so much!
[This message has been edited by Anthony the PC lover (edited 10-15-2000).]
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Jan 2000
Status:
Offline
|
|
I'll give some hints.
Use two variable called line and column to hold the current line and column you are printing to. Use H and W for the size of the box. Print the top and bottom line outside the loop. For each blank line you print a * then W-2 spaces and a *.
Code:
for(column=0;column<W;column++)printf("*");
printf("\n");
for(line=1;line<H-2;line++){
printf("*");
for(colume=1;column<W-2;column++)printf(" ");
printf("*\n");
}
for(column=0;column<W;column++)printf("*");
printf("\n");
But after typing the above I think this is better way to do it.
Code:
char topbottom[1024], middle[1024];
for(i=0;i<W;i++){
topbottom[i]="*";
middle[i]=" ";
}
middle[0]="*";
middle[W-1]="*";
middle[W]="\n";
middle[W+1]=0;
topbottom[W]="\n";
topbottom[W+1]=0;
printf("%s", topbottom);
for(i=1;i<H-2;i++)sprintf("%s", middle);
printf("%s", topbottom);
I'm sure the second method is "faster". You have to be careful that the buffers are large enough. If not then you can allocate the buffers as needed.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2000
Location: Boston, MA
Status:
Offline
|
|
Excellent hint Scott! It wasnt quite exact, but a few quick modifications and PRESTO! I had empty boxes! Thanks for your help. I've been banging out problems left and right lately at home, and sometimes I get stuck. But then you give me a boot in the ass and I'm on my way, moving along and becoming a C programmer. Your help is great.
Perhaps you can point out my error in the following multiple-file program? This is supposed to prompt the user for two numbers and then compute the Greatest Common Denominator of them. I think my problem lies in gcd.h. I always get caught on them dang header files! whaddya think?
::::::::::::::
gcd.h
::::::::::::::
int gcd(int x, int y);
{
int cand;
cand = min(x,y);
for (i=cand; i>=1; i--)
{
(x % i) == 0;
(y % i) == 0;
}
return (cand);
}
::::::::::::::
p12.c
::::::::::::::
#include <stdio.h>
#include <stdlib.h>
#include "gcd.h"
int main()
{
int a,b,x,y,result;
printf("Please enter two positive integers\n");
scanf("%i",&a);
scanf("%i",&b);
result = cand(x , y);
printf("Greatest common denominator is %i.\n",result);
return(0);
}
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Jan 2000
Status:
Offline
|
|
In the header files you don't need to uses the variable names. In a way the function definitions in the header server as a "template" so the compiler knows what the function call is, what data type the function returns and what data type the function takes as arguments.
In the above you can have all of gcd in the header. I think that "inlines" the function. But if you do that then leave the semi colon off of the the function definition.
You can do this
Code:
::::::::::::::
gcd.h
::::::::::::::
int gcd(int x, int y) /* removed ";" here*/
{
int cand;
cand = min(x,y);
for (i=cand; i>=1; i--)
{
(x % i) == 0;
(y % i) == 0;
}
return (cand);
}
Or this
Code:
::::::::::::::
gcd.h
::::::::::::::
int gcd(int, int); /* removed x and y here. The header file needs to only know what type it is not the name of it*/
::::::::::::::
gcd.c
::::::::::::::
int gcd(int x, int y)
{
int cand;
cand = min(x,y);
for (i=cand; i>=1; i--)
{
(x % i) == 0;
(y % i) == 0;
}
return (cand);
}
HTH
[This message has been edited by Scott_H (edited 10-18-2000).]
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2000
Location: Boston, MA
Status:
Offline
|
|
Hmm, ok I see what you are saying. I was on the right track, at least. Thanks for the tip! I've always had trouble with them dang multi-file programs, especially when writing my own headers. Great, I'l implement this code and mess aound with it. Im definitely learning!
As you can see from the post's timestamp it's 3:10am EST, and I've been up all night banging away these problems. I swear they'll be the death of me! Anyhow, I just now got stuck on this one seemingly simple problem. This is a program that is supposed to be a one-file one-function program which will read in a positive integer, say n, from the keyboard and then compute both the sum of all odd numbers between 1 and n; and the sum of all even numbers between 1 and n. I thought I had it done correctly for sure but I'm getting the wrong output when I run the program. Can you take a look at this? What do you think?
#include <stdio.h>
int main()
{
int x,y,n,ntwo,a=0,b=0;
printf("Please enter a positive integer: ");
scanf("%d",&n);
ntwo=n;
for (x=0; n>0; x++)
{
a=a+n;
n=n+2;
}
ntwo=ntwo-1;
for (y=0; ntwo>0; x++)
{
b=b+ntwo;
ntwo=ntwo-2;
}
if (x==y)
{
printf("The sum of all odd numbers is %d.\n",b);
printf("The sum of all even numbers is %d.\n",a);
}
else
{
printf("The sum of all odd numbers is %d.\n",a);
printf("The sum of all even numbers is %d.\n",b);
}
}
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Jan 2000
Status:
Offline
|
|
I didn't look at it too close but why not try something like this.
Code:
sumodd=0;
sumeven=0;
for(x=1;x<=n ; ){
summodd+=x;
x++;
if(x<=n)summeven+=x;
x++;
}
That is just count X up in one loop and add evens and odds as you go. You have to be careful you don't go over n so I put that if statement in the loop. That's off the top of my head so it could be better. Maybe two loops?
Code:
for(x=1;x<=n;x+=2){/* <- add two at a time*/
sumodd+=x;
}
for(x=2 /*or zero*/ ;x<=n;x+=2){/* <- add two at a time*/
sumeven+=x;
}
[This message has been edited by Scott_H (edited 10-21-2000).]
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2000
Location: Boston, MA
Status:
Offline
|
|
Excellent, that second code did the trick nicely. I gave my program a tweak and TADA! I had the sum of all even and odd numbers. Man, you are seriously a genious. You just swing by this thread, smack a quick idea in there without giving it a second thought, and casually leave...AND THE PROGRAM YOU WRITE IS ABSOLUTELY PERFECT TO THE LAST DETAIL!! Not a single error or typo in sight! You are a King among men my friend, a King! Scott you rock the world of C.
(hehe did you notice that part of your last program turned into a smiley face? hahaaha)
[This message has been edited by Anthony the PC lover (edited 10-21-2000).]
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2000
Location: Boston, MA
Status:
Offline
|
|
Scott, the last problem you helped me with before this one, which computes the GCD (greatest common denominator) for a number, does not work. There seems to be some error because "min" isnt declared/defined anywhere. We have solved the proper alogrithm, but the syntax must be incorrect somewhere...what do you think? What wont this puppy work? Does it need another file or function or something? Please help!
::::::::::::::gcd.h::::::::::::::
int gcd(int, int);
::::::::::::::gcd.c::::::::::::::
int gcd(int x, int y)
{
int cand;
cand = min(x,y);
for (i=cand; i>=1; i--)
{
(x % i) == 0;
(y % i) == 0;
}
return (cand);
}
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Jan 2000
Status:
Offline
|
|
Man, you are seriously a genious. You just swing by this thread, smack a quick idea in there without giving it a second thought, and casually leave
Naw. I've been writing C code for a while. For my graduate work I had/have to write software that can read in a 3D surface from CT images and translate/rotate to to match a set of geometically calibrated video images. Put that in your pipe and smoke it;0. BUT my advisor has been at it for almost 30 years. He used to program FORTRN on punch cards. The only reason I know any of this is because I have/had other poeple to help be debug. I've also learned a lot by looking at other peoples code.
Anyway.
In this context you are using "min" as a function. That is the compiler is looking for the funtion min but there isn't one. You have to write it. Put this before gcd().
Code:
int min(int a, int b){
if(a<b)return(a);
return(b);
}
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Jan 2000
Status:
Offline
|
|
We've been moved 
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|