Help with c homework (again)
Author Message Help with c homework (again)

Quote:

> I am taking an otrductory c course.  I am in my last week and only
> needed help from this group once.  I am, however stuck again.  One of
> the questions is:
> Write a function which takes four int arguments, and returns an int
> value.  If the first argument is a zero, the function should return the
> largest of the three remaining arguments.  If the first argument is not
> a zero, the function should return the smallest of the other three
> arguments.  Call the function with arguments from main, and print the
> return value in main.
> I think I will need to use if else in the function.  Does it look like
> I'll need to sort?  Remember I,m just a humble beginner.  Help me!!!

Yes, you'll need to use an if/else in your function.  Sorting is one way
to do it, but probably not the easiest way with just three elements.  I
won't write the program for you, but I'll help you with the logic:

if ( first argument is zero)
set 2nd to largest number.
Is 3rd larger than largest num?  If so, make it the largest number.
Is 4th larger than largest num?  If so, make it the largest number.
Return the largest number
else
set 2nd to smallest number
is 3rd smaller than smallest num?  if so, make it the smallest
is 4th smaller than smallest num?  If so, make it the smallest
return the smallest number

I can think of two other ways to do this, so this isn't the only way.  If
you get stuck, feel free to ask.  Most of the people around here don't
like doing homework for people, but we don't mind answering questions.

-michael
--
Michael Jarvis   |  Finger for PGP Public key  |   QNSnet Technical Support

GCS d H- s+++: g? p+ au a25 w+ v C++++ USL++++\$ P++++ L++>+++ 3- E--- N++ K
W-- M- !V po Y+ t+ 5 j R G+ tv b+++ D++ B- e+>++ u+ h---(*) f+ r+++ !n y+++

Sat, 10 Jan 1998 03:00:00 GMT  Help with c homework (again)
I am taking an otrductory c course.  I am in my last week and only needed help
from this group once.  I am, however stuck again.  One of the questions is:

Write a function which takes four int arguments, and returns an int value.  If the first argument is a zero,
the function should return the largest of the three remaining arguments.  If the first argument is not
a zero, the function should return the smallest of the other three arguments.
Call the function with arguments from main, and print the return value in main.

I think I will need to use if else in the function.
Does it look like I'll need to sort?
Remember I,m just a humble beginner.  Help me!!!

Once again thanks in advance

Sun, 11 Jan 1998 03:00:00 GMT  Help with c homework (again)
#include <stdio.h>

int func(int,int,int,int);

void main(void)
{
printf("%d",func(0,1,2,3);

Quote:
}

int func(int a,int b,int c, int d)
{
int i;
if (a)
{
i=(b<c)?b:c;
return (i<d)?i:d;
Quote:
}

else
{
i=(b>c)?b:c;
return (i>d)?i:d;
Quote:
}
}

Sun, 11 Jan 1998 03:00:00 GMT  Help with c homework (again)
#include <stdio.h>

int func(int,int,int,int);

void main(void)
{
printf("%d",func(0,1,2,3);

Quote:
}

int func(int a,int b,int c, int d)
{
int i;
if (a)
{
i=(b<c)?b:c;
return (i<d)?i:d;
Quote:
}

else
{
i=(b>c)?b:c;
return (i>d)?i:d;
Quote:
}
}

Sun, 11 Jan 1998 03:00:00 GMT  Help with c homework (again)

Actually, he didn't write anything... Just some code that I dared to analyze
on subject of possible errors and mistakes.

Quote:
>#include <stdio.h>

>int func(int,int,int,int);

>void main(void)

REPLACE WITH:
int main(void)

Quote:
>{
>printf("%d",func(0,1,2,3);

REPLACE WITH
printf("%d", func(0, 1, 2, 3));
ADD LINE
return 0;

Quote:
>}

>int func(int a,int b,int c, int d)
>{
>int i;
>if (a)
>{
>i=(b<c)?b:c;
>return (i<d)?i:d;
>}
>else
>{
>i=(b>c)?b:c;
>return (i>d)?i:d;
>}
>}

I don't know what that code was supposed to do, so besides obvious mistakes,
looks good to me. I would just format it a little bit:

int func(int a, int b, int c, int d)
{
int i;
if (a)
{
i = (b < c) ? b : c;
return (i < d) ? i : d;
}
else
{
i = (b > c) ? b : c;
return (i > d) ? i : d;
}

Quote:
}

According to what is written, the func() returns the maximum value of the
second, third, and fourth parameters, if the first parameter is zero, and the
minimal value, otherwise (first parameter is non-zero). Right?

Victor.

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
If it's stupid, but works, it isn't stupid.
Murphy
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Mon, 12 Jan 1998 03:00:00 GMT  Help with c homework (again)

Quote:

>Write a function which takes four int arguments, and returns an int value.  If the first argument is a zero,
>the function should return the largest of the three remaining arguments.  If the first argument is not
>a zero, the function should return the smallest of the other three arguments.
>Call the function with arguments from main, and print the return value in main.

Well, solutions to homework probably shouldn't be provided, but this
solution should lead to some learning if the requestor takes the
time to understand it.

#include <stdio.h>

/*-------------------------------------------------*/
int max (int x1, int x2) {
return (x1 < x2 ? x2 : x1);

Quote:
}

/*-------------------------------------------------*/
int min (int x1, int x2) {
return (x1 < x2 ? x1 : x2);
Quote:
}

/*-------------------------------------------------*/
int max_or_min (int switch, int a, int b, int c) {
if (switch) return max(a, max(b, c));
else return min(a, min(b, c));
/*-------------------------------------------------*/

int main () {
printf ("%3d", max_or_min(1,2,3,4);

Quote:
}

Tue, 13 Jan 1998 03:00:00 GMT  Help with c homework (again)

Hello friends,

I am looking for a Least Squares  Fitting routines in " C" that can fit
upto five polynomials form a set of data (say 20 points).

Your help in this regard is valuable to me now.

Thanks for your co-operation.

Wed, 14 Jan 1998 03:00:00 GMT  Help with c homework (again)

Quote:

>[snip]

>: [snip-snip]

>Is my compiler (gcc) broken or is this program broken (in 3 places)?

>Jerry Bundy

>ftp://ftp.netcom.com/ftp/pub/jb/jbundy

Yes it was, so here is the corrected version--wish I had compiled the
first one before sending it.

#include <stdio.h>

/*-------------------------------------------------*/
int max2 (int x1, int x2) {
return (x1 < x2 ? x2 : x1);

Quote:
}

/*-------------------------------------------------*/
int min2 (int x1, int x2) {
return (x1 < x2 ? x1 : x2);
Quote:
}

/*-------------------------------------------------*/
int max_or_min (int sw, int a, int b, int c) {
if (sw) return max2(a, max2(b, c));
else return min2(a, min2(b, c));
Quote:
}

/*-------------------------------------------------*/

int main () {
int sw = 5;
int x = 5, y = 10, z = 15;
printf ("%3d\n", max_or_min(sw,x,y,z));

Quote:
}

Sat, 17 Jan 1998 03:00:00 GMT  Help with c homework (again)
There are _excellent_ fitting routines, and other numerical routines, in
the book
Numerical Recipes in C
by Press, Flannery, Teukolsky, and Vetterling
ISBN 0-521-35465-X

Along with algorithms, there is extensive and very readable discussion
of stability, reliability, etc.  For what you want to do, look under the
Levenberg-Marquardt method.

Marcus Mendenhall

Sun, 18 Jan 1998 03:00:00 GMT

 Page 1 of 1 [ 12 post ]

Relevant Pages

Powered by phpBB® Forum Software