PDA

View Full Version : C++ Help


Jburks8
04-21-2011, 11:45 PM
Any shot anyone could lend me any C ++ help for school? I'm not very computer savy. The projects are probably a piece of cake for anyone with even a little bit of C++ knowledge.

Here they are:

1). READ THE PROBLEM: Write a program to find the smallest number, the largest number, and the average of any five numbers entered by the user. For example, if the five numbers entered are 1, 2, 3, 4, and 5, the answers should be smallest =1, largest=5, and average=3. (Your program should handle any five numbers from 1 to 1,000, in any sequence entered.)


2). READ THE PROBLEM: Write a program to ask for any amount from 0 to 99. This is the amount of change to be made. The program should calculate the number of quarters, dimes, nickels, and pennies to make the specified change with the fewest coins possible. For example, if the amount is 92 then your program should have output similar to this: Q=3, D=1, N=1. P=2.


Thanks in advance for anyone who may be able to help or offer me any advice!

naez
04-21-2011, 11:48 PM
Those are actually simple, but I am too lazy to help you. When I am too lazy to do something, I post it on www.stackoverflow.com. The nerds there have done like 90% of my programming since they existed.

Just don't say it's HW, make up something.


---

For the first one I'd like store the five numbers in an array, then run them through min() and max() and through a for loop to do the average.


---


For the second one define the value of coins, then take the input int and just go through them.


#define QUARTER 25

// then in main():

int numquarters = 0, numdimes = 0;

int i;
cin >> i;

while ( i != 0)
{
if (i >= QUARTER)
{
i -= QUARTER;
numquarters++;
continue;
}
// dimes etc fall thru
}

printf("Q=%d, D=%d, ...", numquarters, numdimes, ...);


sry lazy

Jburks8
04-22-2011, 12:03 AM
Awesome man, I'll try and post something there for sure. The help that I got from my instructor was this:

#include <iostream.h>

void main()

{int amount, Q, D, N, P;



Q=0; D=0; N=0; P=0;



cout << "Enter amount" << endl;

cin >> amount;

cout<<endl;


if (amount>24) {Q=Q+1; amount=amount-25;}

if (amount>24) {Q=Q+1; amount=amount-25;}

if (amount>24) {Q=Q+1; amount=amount-25;}

if (amount>9) {D=D+1; amount=amount-10;)

// go a few more lines

naez
04-22-2011, 12:04 AM
your instructor makes me want to strangle young children, thats the most noobish code I've ever seen out of someone with a degree in the subject

<iostream> not <iostream.h>, c'mon lets use standards
void main()
he declares QDNP, but then assigns them base value on a different line(s)
no use of overloaded operators like ++ or -=
what a lazy fuckng way to account if there is more than 2 quarters, whats he want 9 if statements for pennies? copypasta that totally violates D.R.Y.
srsly wtf



god this rages me

moklianne
04-22-2011, 09:29 AM
your instructor makes me want to strangle young children, thats the most noobish code I've ever seen out of someone with a degree in the subject

<iostream> not <iostream.h>, c'mon lets use standards
void main()
he declares QDNP, but then assigns them base value on a different line(s)
no use of overloaded operators like ++ or -=
what a lazy fuckng way to account if there is more than 2 quarters, whats he want 9 if statements for pennies? copypasta that totally violates D.R.Y.
srsly wtf



god this rages me

What makes you think he has a degree in this? ;) All the programming instructers I've had at my college were all adjuncts with no relavent degree.
Plus, when teaching its easier to read this way for programming newbs. Besides, things get easier when they learn about for and while loops. I'm under the assumption that he hasn't introduced them yet because he gave them this:
if (amount>24) {Q=Q+1; amount=amount-25;}

if (amount>24) {Q=Q+1; amount=amount-25;}

if (amount>24) {Q=Q+1; amount=amount-25;}

if (amount>9) {D=D+1; amount=amount-10

Ihealyou
04-22-2011, 09:39 AM
Q = amount / 25
amount %= 25
D = amount / 10
amount %= 10
N = amount / 5
P = amount % 5

No loops, and its not retarded.

bman8810
04-22-2011, 09:52 AM
Ya, don't forget that loops come later. We did the same thing in the programming classes I had to take for chemical engineering.

quido
04-22-2011, 10:00 AM
Did they teach you how to define and use variables? How to accept input and generate output?

More importantly are you familiar with integer math on a computer and the concept of modulus? It's one thing to get a code snippet from some guy on a forum and it's another thing to understand what is happening here. I certainly hope you have received more instruction than just that one silly example program.

Have you tried to just do it yourself? Where are you running into problems?

moklianne
04-22-2011, 10:25 AM
If loops haven't been taught yet, that code mentioned in post #2 won't work or at least will raise a question on whether you did it yourself or not.

If the instructer went over flowcharting, definitely do that. It helps.

naez
04-22-2011, 02:19 PM
IDK aboutyou but I love paying for classes that teach me bad habits like copypasta'ing something 9x

President
04-22-2011, 02:23 PM
I'm confused, it's near the end of the semester and this is the problem you are getting? If it is, complain to your school for having a shit program. If it's not, and you are just starting a class, do yourself a favor and figure it out yourself. It's going to be a long class if you can't write that shit down on paper perfectly in less than an hour.

Rogean
04-22-2011, 02:52 PM
amount &= 25
amount &= 10

You mean %= ?

&= is bitwise assignment.

Rogean
04-22-2011, 02:54 PM
Also looking at the way the teacher has given examples of how to do this, I bet you if you try to complicate this up in more efficient ways, hes going to fail you "Not demonstrating what you've learned" which is basically another way of saying you're smarter than him.

naez
04-22-2011, 03:21 PM
Also looking at the way the teacher has given examples of how to do this, I bet you if you try to complicate this up in more efficient ways, hes going to fail you "Not demonstrating what you've learned" which is basically another way of saying you're smarter than him.

tru.dat, when I took python1 I would do fancy pythony things and the professor would always rage and mark me down for really silly things. I ended up with a C in the class and the hardest program we did was like temperature conversions

bman8810
04-22-2011, 03:35 PM
tru.dat, when I took python1 I would do fancy pythony things and the professor would always rage and mark me down for really silly things. I ended up with a C in the class and the hardest program we did was like temperature conversions

Part of getting a good grade in the class is understanding what the professor wants. Should I have to dumb down my homework assignments in order to acquiesce with what the professor wants? No. Do I? Yes, if I want a good grade.

If you aren't sure whether the professor will accept your more advanced version, you need to discuss it with him. Otherwise, man up and don't complain. Most professors are pretty understanding. However, don't expect much leniency if the entire class does something one way and you do it another way and lose credit. That shit is a pain in the ass to grade.

naez
04-22-2011, 03:38 PM
O I wasn't complaining, by the end I was doing random ass spaghetti (which is really hard to do in python) just to piss him off. I won the passive-aggressive rage war we had.

Zereh
04-22-2011, 03:42 PM
Being creative isn't really encouraged in college. They are not there to teach you to think, they are there to hone your memory recall and information retention / regurgitation skills. So whatever a teaching assistant (or if you're lucky, a professor) crams down your throat is what they want handed back to them.

Ihealyou
04-22-2011, 03:53 PM
You mean %= ?

&= is bitwise assignment.

Lol, I didn't even realize I wrote &= until you said that. In my defense, it was on my phone and % and & are both tall and squiggly, so they're basically the same thing anyways.

Jburks8
04-22-2011, 06:29 PM
Thanks everyone for your feedback!

It's an intro to computer science class (I took it in place of a science class). My teacher definitely would probably become suspicious if I used more advanced /efficient ways to solve the problem, so that is definitely not an option, but I think I can get by doing what he considers "correct".

I like what you did Ihealyou, and that makes more sense to me, so I might run with that.

Again, thanks for everyone's feedback/advice. Sorry to post homework questions on a P1999 site :)

Rogean
04-25-2011, 01:01 PM
Lol, I didn't even realize I wrote &= until you said that. In my defense, it was on my phone and % and & are both tall and squiggly, so they're basically the same thing anyways.

Under normal circumstances I'd agree.. but it's typos just like that that make you spend a fucking hour trying to figure out why your program isn't working correctly, debugging the shit out of it... and then it hits you in the face like a brick and you proceed to facepalm for 5 minutes.

Too many times that happens to me. Infact I spent over an hour just last night trying to figure out why some code was acting wonky and its because I did a delete on a char pointer instead of delete[], in a completely different function.

Ihealyou
04-27-2011, 12:55 AM
One night I was out drinking. Being a model student, I decided to work on a c++ project when I got home. That night, I went to bed proud of all the work I accomplished. The next morning, I spent about 2 hours fixing all the typos and dumb code that I wrote the night before. That's when I decided that any language which doesn't allow you to code drunk is a piece of shit that overcomplicates simple tasks.

Ruinous
04-27-2011, 01:09 AM
Step 1: Buy beer.
Step 2: Get drunk.
Step 3: Write everything in Pseudocode.
Step 4: Translate to regular code while sober.
Step 5: .....
Step 6: Profit

Or... experiment more with Ballmer's Peak

Null
04-27-2011, 01:48 AM
http://imgs.xkcd.com/comics/ballmer_peak.png

JayDee
04-27-2011, 02:17 AM
Angwe is back in business

Divarin
04-27-2011, 04:22 PM
here's a simple way to do part 1 without the need for an array

#include <stdlib.h>
#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
int count, min, max, total;
count=0;
total=0;

cout << "Please enter 5 numbers seperated by a space\n";
while ( count < 5 )
{
int number;
cin >> number;
count++;
if ( 1 == count )
{
min=number;
max=number;
}
else
{
if ( number < min )
min = number;
if ( number > max )
max = number;
}
total += number;
}

cout << " Lowest: " << min << endl
<< "Highest: " << max << endl
<< "Average: " << total/5 << endl;

system("pause");

return 0;
}

Truth
02-24-2012, 03:37 PM
So I have never used smart/auto pointers or seen them used in any production code. One of the big woops about them is that when you copy addresses it nulls the first pointer so you can't dangle it. So I was like man let's see if its a big woop after all.

#include <stdio.h>
int main(int argc, char* argv[])
{
int *p1 = new int(5);
int *p2 = p1;
delete p1; // recycle heap cell... p2 is now a dangling pointer

*p2 = 420; // segmentation fault imqo
printf("%p = %d\n", p2, *p2); // but wait... 0x14dc010 = 420?????

p1 = 0x0; // set address of p1 to null
printf("%p = %d", p1, *p1); // good. generates segmentation fault error...

return 0;
}


maybe more complex example is needed to throw off the compiler?? I even compiled with optimization off cuz i thought it would be just removing the delete line since we use it right after
r00t@wutdo:~/dangling$ g++ -g -O0 -fno-inline main.cpp
r00t@wutdo:~/dangling$ ./a.out
0x24d5010 = 420
Segmentation fault
wut da fuk