Go to Post Michigan teams will eat your souls. You have been warned. - MishraArtificer [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 30-12-2009, 23:48
Overyourhead's Avatar
Overyourhead Overyourhead is offline
Josh
AKA: Josh
FTC #0154 (Renegade)
Team Role: Mentor
 
Join Date: Apr 2007
Rookie Year: 2006
Location: Rhode Island
Posts: 46
Overyourhead is a jewel in the roughOveryourhead is a jewel in the roughOveryourhead is a jewel in the rough
Send a message via AIM to Overyourhead
Kind of off topic c programming question.

Sorry if this is off topic, as it is not directly related to FIRST. I've recently started my intro to C programming class.One of my assignments is to write a program that will take 3 number inputs from the user and find the highest and lowest number of the three. I'm only working on the highest number right now.

I've gotten stuck where I am, maybe you guys can help me. Heres what I have so far. Thanks

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
int input_1, /*Input from integer 1*/
input_2, /*Input from integer 2*/
input_3, /*Input from integer 3*/
high_value; /* Highest Value */


/* Collect integers from the user. */

printf("Please enter your first number:");
scanf("%d", &input_1);
printf("Please enter your second number:");
scanf("%d", &input_2);
printf("Please enter your third number:");
scanf("%d", &input_3);

/* Find the largest number *'


if (input_1 >= input_2 && input_1 >= input_3) {
high_value = input_2;

}
else if (input_2 >= input_2 && input_2 >= input_3) {
high_value = input_2;
}

else if (input_3 >= input_1 && input_3 >= input_2) {
high_value = input_3;
}



/*Display the highest and lowest numbers*/
printf("\n\nThe Largest number is %d. \n",&high_value);




return 0;
}
__________________
"Like the WWF, but for smart people." -George HW Bush
2008-2009 Team 154 FTC
RI Regional-Winning Alliance/PTC Design Award
18th Place in Atlanta (Franklin Division)
2007-2008 Team 154 FTC
Winning Alliance/Inspire Award -RI Regional
Semi-Finals MA Regional
Atlanta Quarter Finials 12th in division(Franklin Division)
2006-2007 FVC 3707
Winning Alliance/Connect Award-RI Regional
26th Place in Atlanta
  #2   Spotlight this post!  
Unread 30-12-2009, 23:59
basicxman basicxman is offline
Emily Horsman
FRC #2200 (MMRambotics)
Team Role: Programmer
 
Join Date: Oct 2007
Rookie Year: 2007
Location: Burlington, Ontario
Posts: 971
basicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant futurebasicxman has a brilliant future
Send a message via AIM to basicxman Send a message via MSN to basicxman Send a message via Yahoo to basicxman
Re: Kind of off topic c programming question.

I'm not going to give you code as this is for a school assignment however why not trying looping through the numbers and comparing against the upper and lower bound.

Assign the high value and low value variables to a null value
Loop through each input
Check if the high value is null, if so assign the input to the high value
Check if the low value is null, if so assign the input to the low value
If the lower/upper bounds are not null...
set the high value to the input if the input is greater than the current high value
set the low value to the input if the input is lower than the current low value

Take some time to think over the problem - flowcharts are always helpful.

You also have a few errors in your program...slow down, look at your code. Debugging is an art.

I'll give you a few tips:
Watch for typos - I see an invalid comment
Watch your assignments - looking at the main structure of your code where it compares the numbers, I see an incorrect assignment.

Plus when posting code on the forum please use [ CODE ][ /CODE ] (without spaces). Good luck!
  #3   Spotlight this post!  
Unread 31-12-2009, 00:02
Branden Ghena's Avatar
Branden Ghena Branden Ghena is offline
Previously: tawnos23
FRC #0240 (TEMPEST)
Team Role: College Student
 
Join Date: Nov 2005
Rookie Year: 2004
Location: Houghton, Michigan (MTU)
Posts: 303
Branden Ghena has a spectacular aura aboutBranden Ghena has a spectacular aura aboutBranden Ghena has a spectacular aura about
Re: Kind of off topic c programming question.

Well, from what I can see you have a basic logic issue that probably comes from a series of typos.

Here is your code (note problems in red):
Code:
/* Find the largest number *'


if (input_1 >= input_2 && input_1 >= input_3) {
high_value = input_2;

}
else if (input_2 >= input_2 && input_2 >= input_3) {
high_value = input_2;
}

else if (input_3 >= input_1 && input_3 >= input_2) {
high_value = input_3;
}
This is what the code was probably meant to be (note changes in green):
Code:
/* Find the largest number *'


if (input_1 >= input_2 && input_1 >= input_3) {
high_value = input_1;

}
else if (input_2 >= input_1 && input_2 >= input_3) {
high_value = input_2;
}

else if (input_3 >= input_1 && input_3 >= input_2) {
high_value = input_3;
}
Now, its worth noting that there is a more efficient way to find the highest if you think about it logically.
Code:
/* Find the largest number *'

high_value = input_1;

if (input_2 >= high_value) {
     high_value = input_2;
}

if (input_3 >= high_value) {
    high_value = input_3;
}
But that is not really important because your method should work just fine if the mistypes are corrected.
__________________
Branden Ghena - Michigan Tech Student and Team 240 Alumnus
Working Towards: Electrical Engineering and Computer Engineering Double Major

"All we have to decide is what to do with the time that is given to us." - Gandalf
  #4   Spotlight this post!  
Unread 31-12-2009, 00:08
rsisk's Avatar
rsisk rsisk is online now
The GURU Channel
AKA: Richard Sisk
FRC #2493 (Robokong)
Team Role: Mentor
 
Join Date: Jan 2008
Rookie Year: 2007
Location: Riverside, CA
Posts: 2,749
rsisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond repute
Send a message via MSN to rsisk
Re: Kind of off topic c programming question.

Setup some test cases, try to cover all possibilities:

Code:
case 1st 2nd 3rd  expect
1    0   0   0     0
2    1   2   3     3
3    1   1   3     3
4    3   2   1     3
5    1   3   2     3
6    2   3   1     3
7    3   0   0     3
8    3   3   3     3
9    a   2   ?     not sure
Then run the test cases through your program mentally, i.e.,

Case 1

Code:
if (0 >= 0 && 0 >= 0)
     true, high_value = 0
Case 2
Code:
if (1 >= 2 && 2 >= 3)
     false    
else if (2 >= 2 && 2 >= 3)
          true       false
else if (3 >= 1 && 3 >= 2)
          true      true  high_value = 3
After case 2, you may see your typo in the second test where you compare input_2 to input_2

Run all your test cases and make sure get the results you expect.

Try to include edge conditions, like the case 1 and case 8, where you test upper and lower limits

Force error conditions with the unexpected, like case 9 where non numeric values are input

After all of this, see if you can simplify the code. For example, what if you were dealing with more than three input numbers? The code would get really complex really fast. Maybe there is a better way?
What whould happen if you set high_value to input_1, then compared high_value to input_2, if less, set high_value to input_2. Do the same for input_3.
You could further simplify by putting the values into a loop until an end condition occured.
As you can see, there is more than one way to approach the defined problem.

Hope that helps

Last edited by rsisk : 31-12-2009 at 00:12. Reason: Learned about the CODE tag, sweet!
  #5   Spotlight this post!  
Unread 31-12-2009, 10:38
byteit101's Avatar
byteit101 byteit101 is offline
WPILib maintainer (WPI)
AKA: Patrick Plenefisch
no team (The Cat Attack (Formerly))
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Worcester
Posts: 699
byteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of light
Re: Kind of off topic c programming question.

Quote:
Originally Posted by basicxman View Post
Plus when posting code on the forum please use [ CODE ][ /CODE ] (without spaces). Good luck!
or, even better, the [ PHP ] Tag (w/o spaces) Although meant for PHP, it (for the most part) correctly highlights C and C++ to some extent, and you can see an additional error: comments end with */ not *'

PHP Code:
#include "stdafx.h"


int _tmain(int argc_TCHARargv[])
{
int input_1/*Input from integer 1*/
input_2/*Input from integer 2*/
input_3/*Input from integer 3*/
high_value/* Highest Value */


/* Collect integers from the user. */

printf("Please enter your first number:");
scanf("%d", &input_1);
printf("Please enter your second number:");
scanf("%d", &input_2);
printf("Please enter your third number:");
scanf("%d", &input_3);

/* Find the largest number *'


if (input_1 >= input_2 && input_1 >= input_3) {
high_value = input_2;

}
else if (input_2 >= input_2 && input_2 >= input_3) {
high_value = input_2;
}

else if (input_3 >= input_1 && input_3 >= input_2) {
high_value = input_3;
}



/*Display the highest and lowest numbers*/
printf("\n\nThe Largest number is %d. \n",&high_value);




return 
0;

__________________
Bubble Wrap: programmers rewards
Watchdog.Kill();
printf("Watchdog is Dead, Celebrate!");
How to make a self aware robot: while (∞) cout<<(sqrt(-∞)/-0);
Previously FRC 451 (The Cat Attack)
Now part of the class of 2016 at WPI & helping on WPILib
  #6   Spotlight this post!  
Unread 03-01-2010, 01:09
daltore's Avatar
daltore daltore is offline
Electronics/programming/design
AKA: Aaron Osmer
FRC #3529 (ausTIN CANs)
Team Role: Mentor
 
Join Date: Dec 2007
Rookie Year: 2007
Location: San Antonio, TX
Posts: 272
daltore has a spectacular aura aboutdaltore has a spectacular aura aboutdaltore has a spectacular aura about
Send a message via AIM to daltore Send a message via MSN to daltore Send a message via Yahoo to daltore
Re: Kind of off topic c programming question.

Also, there's another bit of a small discrepancy here that you made logically, but doesn't actually work. First, a short description of pointers...

Pointers are basically variables that don't take up any space. They don't store data, they store a memory address that points to something that does store data. So if I had an unsigned character variable Var, and it had a value of 123, I could make a pointer Pnt of the same type (unsigned char), that wouldn't take up the full 8 bits of memory space, but would instead redirect the computer to read from wherever Var is stored. There are two things you have to deal with (and they get very tricky) when working with pointers; lvalues and rvalues.

lvalues (left values) are what you see on the left side of an equal sign, you're taking an actual quantity and telling the computer where to put it. The lvalue is just the address of the variable. rvalues (right values), are the actual quantity value that you're reading or putting into another variable. When working with pointers, you have to specify, because it changes whether you are reading or writing to the variable which it points to. You specify an address (lvalue) by an ampersand (&), and you specify the value (rvalue) as an asterisk (*).

This brings us to scanf. You call scanf as "scanf("%c", &Var);" This is because you're writing a value to Var, and you need to know where to put it. However, when you want to display the same variable with printf, it needs to know what it's showing, not where to look, so you use the rvalue, but there's no asterisk needed, because normal (non-pointer) variables are assumed to either be an rvalue or an lvalue depending on where they show up. As a parameter to a function (something in between parentheses, no joke intended), it's assumed you want to give that function data, so the computer assumes an rvalue. However, with scanf, you really want to know where to put the data, so you have to specify with an &. You don't for any other common function.

This was a long way of pointing out that you don't need an & in the printf() statement at the end. Trust me, I've made this mistake plenty of times. But the address is actually a really big number that's stored in the register, not memory, which is why you get such a weird and huge value (which is also different every time) if you try to read data from that. Otherwise, just slips and typos here and there, stuff every good programmer does every once in a while.
  #7   Spotlight this post!  
Unread 03-01-2010, 20:02
EricS-Team180's Avatar
EricS-Team180 EricS-Team180 is offline
SPAM, the lunchmeat of superheroes!
AKA: Eric Schreffler
FRC #0180 (SPAM)
Team Role: Engineer
 
Join Date: Apr 2002
Rookie Year: 2001
Location: Stuart, Florida
Posts: 561
EricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond repute
Re: Kind of off topic c programming question.

Quote:
Originally Posted by basicxman View Post
...slow down, look at your code. Debugging is an art.
...ahhh true wisdom basicxman!

I'll add a 2 word hint: bubble sort ...a classic approach to learn.
__________________

Don't PANIC!
S. P. A. M.
  #8   Spotlight this post!  
Unread 05-01-2010, 18:19
TDohse TDohse is offline
Registered User
AKA: Thomas
no team (NI)
 
Join Date: Apr 2008
Rookie Year: 2008
Location: Austin, TX
Posts: 39
TDohse is an unknown quantity at this point
Re: Kind of off topic c programming question.

Quote:
Originally Posted by daltore View Post
Pointers are basically variables that don't take up any space. They don't store data, they store a memory address that points to something that does store data. So if I had an unsigned character variable Var, and it had a value of 123, I could make a pointer Pnt of the same type (unsigned char), that wouldn't take up the full 8 bits of memory space, but would instead redirect the computer to read from wherever Var is stored.
Actually, pointers take up memory space just like any other variable. The size will vary depending on the architecture you're on. A pointer to a char might take 32 or 64 bits on a modern desktop system.

If you're curious, run this code:
Code:
char *fooPtr;
printf("fooPtr is %i bytes\n", sizeof(fooPtr));
  #9   Spotlight this post!  
Unread 09-01-2010, 00:08
daltore's Avatar
daltore daltore is offline
Electronics/programming/design
AKA: Aaron Osmer
FRC #3529 (ausTIN CANs)
Team Role: Mentor
 
Join Date: Dec 2007
Rookie Year: 2007
Location: San Antonio, TX
Posts: 272
daltore has a spectacular aura aboutdaltore has a spectacular aura aboutdaltore has a spectacular aura about
Send a message via AIM to daltore Send a message via MSN to daltore Send a message via Yahoo to daltore
Re: Kind of off topic c programming question.

Ah, I stand corrected. But the rest is correct, yes?
  #10   Spotlight this post!  
Unread 09-01-2010, 00:28
Chris27's Avatar
Chris27 Chris27 is offline
Registered User
AKA: Chris Freeman
FRC #1625 (Winnovation)
Team Role: Alumni
 
Join Date: Mar 2005
Rookie Year: 2004
Location: Mountain View
Posts: 196
Chris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant future
Re: Kind of off topic c programming question.

Quote:
Originally Posted by daltore View Post
lvalues (left values) are what you see on the left side of an equal sign, you're taking an actual quantity and telling the computer where to put it. The lvalue is just the address of the variable. rvalues (right values), are the actual quantity value that you're reading or putting into another variable. When working with pointers, you have to specify, because it changes whether you are reading or writing to the variable which it points to. You specify an address (lvalue) by an ampersand (&), and you specify the value (rvalue) as an asterisk (*).
An l-value is not an address, but something that has an address. For example, assume x was declared as "int x". x is an l-value, however, &x is not (that would be an r-value as all numbers are). Conversely, if x was declared as "int * x", both x and *x are l-values. For me it's easiest to think of l-values as anything that can be placed to the left of an '=' and r-values as anything that cannot.

Last edited by Chris27 : 09-01-2010 at 00:34.
  #11   Spotlight this post!  
Unread 09-01-2010, 22:34
daltore's Avatar
daltore daltore is offline
Electronics/programming/design
AKA: Aaron Osmer
FRC #3529 (ausTIN CANs)
Team Role: Mentor
 
Join Date: Dec 2007
Rookie Year: 2007
Location: San Antonio, TX
Posts: 272
daltore has a spectacular aura aboutdaltore has a spectacular aura aboutdaltore has a spectacular aura about
Send a message via AIM to daltore Send a message via MSN to daltore Send a message via Yahoo to daltore
Re: Kind of off topic c programming question.

Quote:
An l-value is not an address, but something that has an address. For example, assume x was declared as "int x". x is an l-value, however, &x is not (that would be an r-value as all numbers are). Conversely, if x was declared as "int * x", both x and *x are l-values. For me it's easiest to think of l-values as anything that can be placed to the left of an '=' and r-values as anything that cannot.
Well what do you know, you're right. I should really work with pointers more often, I've been out of practice for a few months. I'm just going to stop talking now.
  #12   Spotlight this post!  
Unread 11-01-2010, 09:49
slavik262's Avatar
slavik262 slavik262 is offline
We do what we must because we can.
AKA: Matt Kline
FRC #0537 (Charger Robotics)
Team Role: Alumni
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Sussex, WI
Posts: 310
slavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to behold
Send a message via AIM to slavik262
Re: Kind of off topic c programming question.

The sad thing is after you do these silly sorting assignments you find that qsort() is built into the standard library. :/
__________________
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Off Topic Brett Elflord Chit-Chat 1 30-11-2007 18:05
OFF TOPIC: av switch matt111 Electrical 2 21-03-2004 15:34
Threads getting off topic Mike Soukup CD Forum Support 29 20-08-2001 04:08
chat/off-topic Brandon Martus General Forum 6 06-08-2001 10:10
off topic thread mike o'leary Chit-Chat 4 31-07-2001 22:55


All times are GMT -5. The time now is 01:09.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi