C++ questions - DFWstangs Forums
 
LinkBack Thread Tools Display Modes
post #1 of 24 (permalink) Old 04-18-2005, 09:00 PM Thread Starter
Narcissistic fade away
 
turbostang's Avatar
 
Join Date: Jan 2002
Posts: 17,504
C++ questions

As some of you may know I am attempting to learn some C++. I am working on an assignment that asks for :

User to enter a group of numbers (n) where n is the number of values to be entered. The range is to be between 0 and 20.

The program is to display average, sum, largest and smallest which are all pretty easy BUT the problem I am having is this - It also asks to echo back the input numbers. This is where I am lost. The variable "itemvalue" changes over and over due to an accumulator that is used.

How do you echo back the numbers individually ? I am sure this is an easy fix but I have been staring (sp?) at this forever it seems like and It just isn't making any sense.
turbostang is offline  
Sponsored Links
Advertisement
 
post #2 of 24 (permalink) Old 04-18-2005, 09:22 PM
The token Asian guy
 
propellerhead's Avatar
 
Join Date: Jul 2004
Location: SBRacing
Posts: 8,050
Quote:
Originally Posted by turbostang
As some of you may know I am attempting to learn some C++. I am working on an assignment that asks for :

User to enter a group of numbers (n) where n is the number of values to be entered. The range is to be between 0 and 20.

The program is to display average, sum, largest and smallest which are all pretty easy BUT the problem I am having is this - It also asks to echo back the input numbers. This is where I am lost. The variable "itemvalue" changes over and over due to an accumulator that is used.

How do you echo back the numbers individually ? I am sure this is an easy fix but I have been staring (sp?) at this forever it seems like and It just isn't making any sense.
What do you have so far? You should go through the array of numbers and cout them.

propellerhead is offline  
post #3 of 24 (permalink) Old 04-18-2005, 09:51 PM Thread Starter
Narcissistic fade away
 
turbostang's Avatar
 
Join Date: Jan 2002
Posts: 17,504
Quote:
Originally Posted by propellerhead
What do you have so far? You should go through the array of numbers and cout them.

When you enter the first number it is assigned to cin >> itemValue;

then you set the largest and smallest to the first input value -


accumulator = accumulator + itemValue ;

minValue = itemValue ;

largestSoFar = itemValue;

------------

then at the end of the program I just -

cout << largestSoFar << endl;
cout << minValue << endl;

and I have to cout the other numbers (up to 20 different ones) but they are all assigned by the itemvalue variable which changes each time with the accumulator... that is the part I dont understand.

How do I cout each individual number when the are all initially assigned to "itemValue"??
turbostang is offline  
 
post #4 of 24 (permalink) Old 04-18-2005, 10:08 PM
The token Asian guy
 
propellerhead's Avatar
 
Join Date: Jul 2004
Location: SBRacing
Posts: 8,050
Hold each value in an array. Declare an array of size 20. Then using a loop structure, accept the user inputs into the array, sort of like this:

int my_array[20];
for (i=0; i<20; i++)
cin >> itemValue[i];

You'll have to set up some method to bail out of the loop early. Like if the user enters '*' instead of a number. Save the value of i at the time of exit so you'll know how many numbers were entered. Then, have another loop to go through your array from 0..i, cout the value, and figure out your min, max, avg and sum.

propellerhead is offline  
post #5 of 24 (permalink) Old 04-19-2005, 11:56 PM
 
Join Date: Mar 2003
Posts: 1,394
Quote:
Originally Posted by propellerhead
Hold each value in an array. Declare an array of size 20. Then using a loop structure, accept the user inputs into the array, sort of like this:

int my_array[20];
for (i=0; i<20; i++)
cin >> itemValue[i];

You'll have to set up some method to bail out of the loop early. Like if the user enters '*' instead of a number. Save the value of i at the time of exit so you'll know how many numbers were entered. Then, have another loop to go through your array from 0..i, cout the value, and figure out your min, max, avg and sum.
That might be a biiiiiiiiiiiiiiiiiiiiiiiiiiiit over his head if he is learning C++.

I'll install my compiler now, and write the program. I'll send it to you if you want, but I'd rather just answer your question(s) so you actually learn. However, if you are like me you will learn by looking at others code.

I should be done later tonite, if not tomorow at the latest.
xtremcoder is offline  
post #6 of 24 (permalink) Old 04-20-2005, 09:50 PM Thread Starter
Narcissistic fade away
 
turbostang's Avatar
 
Join Date: Jan 2002
Posts: 17,504
Quote:
Originally Posted by xtremcoder
That might be a biiiiiiiiiiiiiiiiiiiiiiiiiiiit over his head if he is learning C++.

I'll install my compiler now, and write the program. I'll send it to you if you want, but I'd rather just answer your question(s) so you actually learn. However, if you are like me you will learn by looking at others code.

I should be done later tonite, if not tomorow at the latest.

Dude you just dont know. ANY help is greatly appreciated. I have a program written for the most part that I can send you - The teacher doesn't want any thing in the program that hasn't been discussed in class.

[email protected]
turbostang is offline  
post #7 of 24 (permalink) Old 04-20-2005, 09:56 PM
#4 Best QB Ever
 
That_Is_My_El_Camino's Avatar
 
Join Date: Apr 2004
Location: Land of the Free
Posts: 29,339
Are you supposed to print out the numbers that were entered all at once at the end of the program? I can see how it'd be pretty tough to do if you don't store 'em in an array...


Quote:
Originally Posted by DON SVO View Post
Women: vaginal life support.
That_Is_My_El_Camino is offline  
post #8 of 24 (permalink) Old 04-20-2005, 10:06 PM Thread Starter
Narcissistic fade away
 
turbostang's Avatar
 
Join Date: Jan 2002
Posts: 17,504
Well.. I just found out that she changed the assignment mid-stream and we are in fact NOT supposed to echo back the numbers even though the original assignment still says to do so.
She email'd me and said that it requires "tools" that we haven't used just yet - SO I looked ahead in the book and sure enough the next assignment covers arrays...
turbostang is offline  
post #9 of 24 (permalink) Old 04-20-2005, 10:59 PM
 
Join Date: Mar 2003
Posts: 1,394
Quote:
Originally Posted by turbostang
Dude you just dont know. ANY help is greatly appreciated. I have a program written for the most part that I can send you - The teacher doesn't want any thing in the program that hasn't been discussed in class.

[email protected]
send it to [email protected] along with any questions you might have.
xtremcoder is offline  
post #10 of 24 (permalink) Old 04-21-2005, 07:07 AM
The token Asian guy
 
propellerhead's Avatar
 
Join Date: Jul 2004
Location: SBRacing
Posts: 8,050
Quote:
Originally Posted by turbostang
Well.. I just found out that she changed the assignment mid-stream and we are in fact NOT supposed to echo back the numbers even though the original assignment still says to do so.
She email'd me and said that it requires "tools" that we haven't used just yet - SO I looked ahead in the book and sure enough the next assignment covers arrays...
It was probably a mind game to make you bang your head against the wall trying to figure out how to do a very difficult or almost impossible task. Then they introduce arrays and you'll be like "Wow! Arrays are great!"

Didn't they do that on a previous homework? Where the best solution was something you guys were about to cover? Yea, that fractional part truncation thing.

propellerhead is offline  
post #11 of 24 (permalink) Old 04-21-2005, 05:37 PM Thread Starter
Narcissistic fade away
 
turbostang's Avatar
 
Join Date: Jan 2002
Posts: 17,504
Quote:
Originally Posted by propellerhead
It was probably a mind game to make you bang your head against the wall trying to figure out how to do a very difficult or almost impossible task. Then they introduce arrays and you'll be like "Wow! Arrays are great!"

Didn't they do that on a previous homework? Where the best solution was something you guys were about to cover? Yea, that fractional part truncation thing.

Yeah.. it was the "floor" function in the Cmath library.. pretty odd to me.
turbostang is offline  
post #12 of 24 (permalink) Old 04-21-2005, 09:09 PM
IA2
 
mikeb's Avatar
 
Join Date: Mar 2001
Posts: 22,413
Just hang in there Brooks. C & C++ & object oriented programming seems quite foreign at first but you'll find that it makes more sense as you go along.

Until you get to pointers

PM me when that happens and i'll help ya.

And yes, an array is what you needed to keep up with the input values.
mikeb is offline  
post #13 of 24 (permalink) Old 04-21-2005, 09:30 PM
No Cerveza... No Trabajo
 
01WhiteCobra's Avatar
 
Join Date: Jun 2002
Location: Where's my beer?
Posts: 21,924
Quote:
Originally Posted by mikeb
Just hang in there Brooks. C & C++ & object oriented programming seems quite foreign at first but you'll find that it makes more sense as you go along.

Until you get to pointers

PM me when that happens and i'll help ya.

And yes, an array is what you needed to keep up with the input values.
Pointers is when it all made sense to me...

My 401K is now a 400K (was 301K)
01WhiteCobra is offline  
post #14 of 24 (permalink) Old 04-21-2005, 09:36 PM Thread Starter
Narcissistic fade away
 
turbostang's Avatar
 
Join Date: Jan 2002
Posts: 17,504
Quote:
Originally Posted by mikeb
Just hang in there Brooks. C & C++ & object oriented programming seems quite foreign at first but you'll find that it makes more sense as you go along.

Until you get to pointers

PM me when that happens and i'll help ya.

And yes, an array is what you needed to keep up with the input values.
Well - I keep telling myself that at some point in time it will "click" and all make sense to me.. until then I keep pluggin' right along. hahaha. Pointers? Damn there is another new term
turbostang is offline  
post #15 of 24 (permalink) Old 04-21-2005, 09:43 PM
No Cerveza... No Trabajo
 
01WhiteCobra's Avatar
 
Join Date: Jun 2002
Location: Where's my beer?
Posts: 21,924
Quote:
Originally Posted by turbostang
Well - I keep telling myself that at some point in time it will "click" and all make sense to me.. until then I keep pluggin' right along. hahaha. Pointers? Damn there is another new term
It will click. When it does you'll look at something like this and think... pfft... that shit is easy. Keep at it. The world needs good programmers.

double wksnum(wks1* w1, wks2* w2)
{
if (w1->t == WKS_DBL || w1->t == WKS_FRM)
return (*(double *)w2->d);
else if (w1->t == WKS_INT)
return ((double)(*(short*)w2->d));
return ((double) 0.0);
}

My 401K is now a 400K (was 301K)
01WhiteCobra is offline  
post #16 of 24 (permalink) Old 04-21-2005, 09:48 PM Thread Starter
Narcissistic fade away
 
turbostang's Avatar
 
Join Date: Jan 2002
Posts: 17,504
Quote:
Originally Posted by 01WhiteCobra
It will click. When it does you'll look at something like this and think... pfft... that shit is easy. Keep at it. The world needs good programmers.

double wksnum(wks1* w1, wks2* w2)
{
if (w1->t == WKS_DBL || w1->t == WKS_FRM)
return (*(double *)w2->d);
else if (w1->t == WKS_INT)
return ((double)(*(short*)w2->d));
return ((double) 0.0);
}

Funny thing is ...some of that already makes sense ! I wish I had started this mess a LONG time ago ..
turbostang is offline  
post #17 of 24 (permalink) Old 04-22-2005, 10:19 AM
IA2
 
mikeb's Avatar
 
Join Date: Mar 2001
Posts: 22,413
Quote:
Originally Posted by 01WhiteCobra
Pointers is when it all made sense to me...
A lot of people do OK until they get to pointers, and then they freak out.

Or a pointer to an array of pointers...... or a function pointer.....

or a NULL pointer
mikeb is offline  
post #18 of 24 (permalink) Old 04-22-2005, 02:10 PM Thread Starter
Narcissistic fade away
 
turbostang's Avatar
 
Join Date: Jan 2002
Posts: 17,504
I really appreciate the words of encouragement from you guys. Now that I know that there are a few programmers in our forum - I know who to bug!
turbostang is offline  
post #19 of 24 (permalink) Old 04-22-2005, 03:08 PM
#4 Best QB Ever
 
That_Is_My_El_Camino's Avatar
 
Join Date: Apr 2004
Location: Land of the Free
Posts: 29,339
Quote:
Originally Posted by mikeb
A lot of people do OK until they get to pointers, and then they freak out.

Or a pointer to an array of pointers...... or a function pointer.....

or a NULL pointer
That crap scared me the first time I saw it, but by the time I got back into C/C++ in high school, I had a little better handle on it than before.


Quote:
Originally Posted by DON SVO View Post
Women: vaginal life support.
That_Is_My_El_Camino is offline  
post #20 of 24 (permalink) Old 04-22-2005, 11:54 PM
 
Join Date: Mar 2003
Posts: 233
i've been doing it since 8th grade

yea, if you stay at it..it'll get easier
and dont fret if you dont understand something at this point, it will all make sense the more you advance and the more you practise.

lol i rember when i was a freshman in high school i made my own little instant messenger for me and my cousin, only. dad took away aim and internet privllages..so..i made my own ..including my own web browser (just used visual for that part..dindt really code much for that)
ispy_malik is offline  
post #21 of 24 (permalink) Old 05-01-2005, 12:57 PM Thread Starter
Narcissistic fade away
 
turbostang's Avatar
 
Join Date: Jan 2002
Posts: 17,504
Quote:
Originally Posted by xtremcoder
send it to [email protected] along with any questions you might have.

I am sending you some mail
turbostang is offline  
post #22 of 24 (permalink) Old 05-02-2005, 04:01 AM
Formerly 5.0Vert
 
Tangstang's Avatar
 
Join Date: Dec 2002
Posts: 9,823
i remember c++.....i wish i could still do that but its been a couple years since that class and i've forgotten most of it

Tangstang is offline  
post #23 of 24 (permalink) Old 05-02-2005, 06:48 AM
The token Asian guy
 
propellerhead's Avatar
 
Join Date: Jul 2004
Location: SBRacing
Posts: 8,050
Who else here coded in FORTRAN77? (/me raises hand.)

propellerhead is offline  
post #24 of 24 (permalink) Old 05-02-2005, 09:36 AM
Time Served
 
Join Date: Jan 2002
Location: Bedford, TX
Posts: 761
Pointers are good. I would not do anything in Java because they don't have them. It just doesn't make sense.
Sami is offline  
Sponsored Links
Advertisement
 
Reply

Bookmarks

Quick Reply
Message:
Options

Register Now



In order to be able to post messages on the DFWstangs Forums forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.

User Name:
Password
Please enter a password for your user account. Note that passwords are case-sensitive.

Password:


Confirm Password:
Email Address
Please enter a valid email address for yourself.

Email Address:
OR

Log-in










Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page
Display Modes
Linear Mode Linear Mode



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

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On

 
For the best viewing experience please update your browser to Google Chrome