The programming help thread!

Started by Dark Fact, 06/01/2007, 01:46 AM

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

Dark Fact

I've decided to start up this here thread in regards to programming, software design, the whole shibang.

Recently, I decided to dust off my old C programming textbook and get the old programming skills running again.  Now, for the record, I've completed my courses at my university and I'm getting my degree next week.  However, my programming skills have been dropping the past couple years and I want to get back on track when it comes time to get a good job in my programming profession.  That said, I've recently decided to go back to university for one more year to take a few extra credit courses in order to fulfill the requirements for a 4 year bachelors in Business Computing (the degree I'm getting next week is a 3 year bachelors in Business Computing).  Since I've already completed all my computing courses, all I can take is a couple of history and literature ones for the time being until I've got all my credits for a 4 year degree.  My goal is to have my programming skills sharpened by next spring so I can be ready to tackle the work force full time.

That said, I'm going to run into a few problems with my programs every now and then such as a function that just won't work for some reason, a stubborn loop, or incorrect data.  That's where I will ask you guys for advice.  If you can point out some things wrong in my programs, I would like to hear it.  Now, for the record, I don't expect anyone here to write the programs for me.  That is supposed to be my job and I need the experience.  All I'm saying is that if I come to a major snag that is beyond my comprehension, I would like a little help. :)

Finishing university is one thing, but getting a major job in my profession is a whole different ball game.  If I'm gonna go into the work force, I wanna go prepared.

So, with that said, let's get down to business.  I have a little program that I was working on in C today that is none other than the familiar "Fahrenheit to Celsius" conversion.  Now, for the record, the program works.  The problem I have with it is that I'm not sure if the output is correct.  The output seems correct but there was a stipulation in the textbook question saying that the celsius values need to be rounded to the nearest integer.  Oh, and by the way, the values have to be displayed as integers.  Here is the output:

CELSIUS     FAHRENHEIT
-----------------------
     -6             20
     -3             25
     -1             30
      1             35
      4             40
      7             45
     10            50
     12            55
     15            60
     18            65
     21            70
     23            75
     26            80
     29            85
     32            90
     35            95
     37           100

The Fahrenheit values are correct as the question asked me to record the values in five degree intervals.  It is the Celsius values that worry me.  For the hell of it, I converted my "int" values into "float" to test to see if they were more accurate and sure enough, they were.  It's just these integer values that leave me inconclusive.  This is why I need a second opinion on the matter.  Are these Celsius values accurate as an integer definition or are a few of the values not rounded properly?

I'm heading to bed now so I won't be back till tomorrow morning to check what you guys have said.  Any help is appreciated! Thanks. :)
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Hobo Xiphas

#1
You have to use float\double for the variables or else you will get incorrect values since the conversion uses division and it will truncate if you throw it into an int.

Use the truncf\trunc function to round off floats\doubles to the nearest integer respectively after you have done the conversion.

EDIT: I checked the libc man and the above won't work, you'll probably have to use an if structure with floor and ceil of some sort to get it working unless my horrible knowledge of C is making me forget a function in math.h.

Keranu

This thread is screaming nodtveidt.
Quote from: TurboXray on 01/02/2014, 09:21 PMAdding PCE console specific layer on top of that, makes for an interesting challenge (no, not a reference to Ys II).
IMG
Click the banner to learn more about Alex Chiu and his "immortality rings"

offsidewing

Sheeee-it.  I got some programming knowledge for you.

Gorillas on Qbasic is the bomb.  Nibbler, not so much.

Oh how the Radioshack at the mall hated me C:\format bitches!!!

Gorillas ranks second on my coolest games ever made right behind Oregon Trail.

Kitsunexus

Forgive me, my brain is on par with the Bubble System.
THE CHEMICAL BROTHERS RULE!

Seldane

#5
Quote from: offsidewing on 06/01/2007, 09:50 AMSheeee-it.  I got some programming knowledge for you.

Gorillas on Qbasic is the bomb.  Nibbler, not so much.

Oh how the Radioshack at the mall hated me C:\format bitches!!!

Gorillas ranks second on my coolest games ever made right behind Oregon Trail.
Dude, Nibbler ROCKS!! Especially at speed 99. At least on a 25MHz 386. It was too ****ing fast on 486's.
Quote from: Seldane on 04/21/2007, 07:28 PMDVDs are for suckers. Illegally pirated and stolen videos all the way. No menus. No "DO NOT PIRATE THIS!" screens. No fuss. Only perfection. I honestly only pirate movies because that "don't pirate this" screen annoys me. :wink:
IMG
Indeed, it's AV time. Check out: IMG! Sir, the door was open.

offsidewing

Quote from: Seldane on 06/01/2007, 11:58 AM
Quote from: offsidewing on 06/01/2007, 09:50 AMSheeee-it.  I got some programming knowledge for you.

Gorillas on Qbasic is the bomb.  Nibbler, not so much.

Oh how the Radioshack at the mall hated me C:\format bitches!!!

Gorillas ranks second on my coolest games ever made right behind Oregon Trail.
Dude, Nibbler ROCKS!! Especially when speed 99. At least on a 25MHz 386. It was too ****ing fast on 486's.
heck, we jumped from a 286 with upgraded 640K of RAM right to a blazing 486SX-33 with 4 whole megs of ram and a brusing 1 meg of video ram.  When I got into CAD (and Xwing), we got the clock tripler.

Dark Fact

Quote from: XiphasI checked the libc man and the above won't work, you'll probably have to use an if structure with floor and ceil of some sort to get it working unless my horrible knowledge of C is making me forget a function in math.h.
The requirement for the question was to have the entire output be in integers.  It also said to "round to the nearest integer".  This is a Chapter 6 question in my textbook that says I should only use the header <stdio.h>, a for loop, and integer values.  I'm not to use any if structures, ceil, floor or extra headers.  That output that you see in my first post is the best I got from it.  However, working out the values on a calculator, some celsius values don't appear properly rounded off like "-6", "-3", and "37".  When I put the values in float, the values are correct.  It's just in int that don't appear correct to me.  If this is the way C interprets the values in int, then there is nothing else I can do with my program.  However, if there is another way to make the values appear properly rounded in int, I would like to know.
Quote from: offsidewingSheeee-it.  I got some programming knowledge for you.

Gorillas on Qbasic is the bomb.  Nibbler, not so much.

Oh how the Radioshack at the mall hated me C:\format bitches!!!

Gorillas ranks second on my coolest games ever made right behind Oregon Trail.
Oh my God.  Those games bring back memories! I loved playing Gorillas in QBasic.  It was WORMS before WORMS! :D
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

akamichi

Quote from: Dark Fact on 06/01/2007, 01:00 PMThe requirement for the question was to have the entire output be in integers.  It also said to "round to the nearest integer".  This is a Chapter 6 question in my textbook that says I should only use the header <stdio.h>, a for loop, and integer values.  I'm not to use any if structures, ceil, floor or extra headers.  That output that you see in my first post is the best I got from it.  However, working out the values on a calculator, some celsius values don't appear properly rounded off like "-6", "-3", and "37".  When I put the values in float, the values are correct.  It's just in int that don't appear correct to me.  If this is the way C interprets the values in int, then there is nothing else I can do with my program.  However, if there is another way to make the values appear properly rounded in int, I would like to know.
If you're using ints to do the calculation, then you'll never be able to round the fractional part to the nearest integer because you'll only get whole numbers as a result.  In other words, integer division drops the remainder (truncate).  Now you could do the calculation using floats and then display the floats to make them look like ints.  But that has nothing to do with the calculation, it's just printing them to the screen in a particular format.

However, from what I understand from your initial post, if you're doing straight integer operations, what you have are the answers you should expect when using integer division.

Hope this helps.

Hobo Xiphas

Quote from: Dark Fact on 06/01/2007, 01:00 PMIt also said to "round to the nearest integer".
I can't see how you can get accurate results with the constraints the book gives you.  What you have now is the best you can do.

offsidewing

Quote from: Dark Fact on 06/01/2007, 01:00 PMOh my God.  Those games bring back memories! I loved playing Gorillas in QBasic.  It was WORMS before WORMS! :D
There's just something about lobbing bananas across a 14 inch radiation emitter.  Yeah, I played the hell out of it (with a buddy of course) until I found the coolness of Red Storm Rising.  It was only replaced by 688 Attack Sub.

Dark Fact

Anyone here know what the math formula is to calculate monthly sales projections? I need it in order to finish the last question on Chapter 6 of my programming textbook.  The whole program for that question is already done.  I just don't know what the monthly sales forecast formula is. ](*,)
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

offsidewing

Quote from: Dark Fact on 06/04/2007, 11:17 PMAnyone here know what the math formula is to calculate monthly sales projections? I need it in order to finish the last question on Chapter 6 of my programming textbook.  The whole program for that question is already done.  I just don't know what the monthly sales forecast formula is. ](*,)
3.14r^2.  or is it y=mx+b?  oh, my favorite. MV=SPANK better know as f=m(dv/dt)=ma

Like my chinese Trig professer always used to say, "I like to see Pi."  Buddy, you and me both.

nat

Where the fuck did this thread come from? It just appeared out of the blue right before my eyes with 11 new messages. Now I know what stevek666 is talking about. To be honest, I just thought he was mental.

Quote from: Keranu on 06/01/2007, 02:12 AMThis thread is screaming nodtveidt.
Really? I didn't hear anything.

Quote from: offsidewing on 06/01/2007, 09:50 AMGorillas ranks second on my coolest games ever made right behind Oregon Trail.
I'll second you there (on the Oregon Trail deal). The Apple // version rules ALL. I still play it sometimes for old time's sake on my Apple //c+. Yes, I am being completely serious. The Macintosh version was OK, but too graphical and hunting wasn't as cool as in the Apple // version; you couldn't walk around the forest and shoot different directions.

Quote from: Dark Fact on 06/04/2007, 11:17 PMAnyone here know what the math formula is to calculate monthly sales projections? I need it in order to finish the last question on Chapter 6 of my programming textbook.  The whole program for that question is already done.  I just don't know what the monthly sales forecast formula is. ](*,)
Yes, if it's as simple as what I think you're asking.

My entire job revolves around doing just that. That's right, I sit at a computer all day tabulating sales data and projecting sales and profit margins for the next week, month, and year. Then I devise new and illegal interesting ways to improve them. And in between when my boss isn't around I log on to www.pcengine-fx.com/forums and make fun of people.

(Sales MTD / Days of month that have passed) * number of days in month

For example, to project sales for June as of right now, and assuming my MTD sales are $50,000:

(50,000 / 3 (technically the 4th day isn't over so we won't count it) ) * 30 (30 days in June) = a projection of $500,000 for the month.

offsidewing

Quote from: nat on 06/04/2007, 11:42 PMI'll second you there (on the Oregon Trail deal). The Apple // version rules ALL. I still play it sometimes for old time's sake on my Apple //c+. Yes, I am being completely serious. The Macintosh version was OK, but too graphical and hunting wasn't as cool as in the Apple // version; you couldn't walk around the forest and shoot different directions.
The MAC version of hunting licked balls.  Who the heck wants to shoot a 1 frame per minute shotgun from a fixed firing position.  Gimme a rifle, a wagon full of bullets, one color, and bring on the BEARS!

Around my office, I'm the boss.  Anyone caught posting on pcengine-fx.com during work hours (like I sometimes do) would immediately get a raise and promoted.  Sadly, no cool people work for me...

Nazi NecroPhile

Quote from: nat on 06/04/2007, 11:42 PMWhere the fuck did this thread come from? It just appeared out of the blue right before my eyes with 11 new messages. Now I know what stevek666 is talking about. To be honest, I just thought he was mental.
Great, now it's spreading.  Both nat and stevek666 are going nuckin' futs.

Quote from: offsidewing on 06/04/2007, 11:49 PMAround my office, I'm the boss.  Anyone caught posting on pcengine-fx.com during work hours (like I sometimes do) would immediately get a raise and promoted.  Sadly, no cool people work for me...
Are you hiring?  'Cause I'd be willing to relocate for some of this easy money.  :lol:
Ultimate Forum Bully/Thief/Saboteur/Clone Warrior! BURN IN HELL NECROPHUCK!!!

Dark Fact

Quote from: natYes, if it's as simple as what I think you're asking.

My entire job revolves around doing just that. That's right, I sit at a computer all day tabulating sales data and projecting sales and profit margins for the next week, month, and year. Then I devise new and illegal interesting ways to improve them. And in between when my boss isn't around I log on to www.pcengine-fx.com/forums and make fun of people.

(Sales MTD / Days of month that have passed) * number of days in month

For example, to project sales for June as of right now, and assuming my MTD sales are $50,000:

(50,000 / 3 (technically the 4th day isn't over so we won't count it) ) * 30 (30 days in June) = a projection of $500,000 for the month.
The requirements for the question don't mention anything about specific days in a month.  The months are just given standard numbers from 1-6 since the question is for a six month projection.  Plus, there is the matter of growth rate percentages to deal with.  For instance, if the minimum assumed growth rate is 7%, then I need to post 6 month sales forecasts with the added growth rate percentage.  The growth rate goes up to a maximum of 4%.  So therefore, 7% would go to 11% or 5% would go to 9%.  I need a formula that disregards specific days in a month and takes into account growth rate percentages.
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

Quote from: Dark Fact on 06/05/2007, 12:32 AMThe requirements for the question don't mention anything about specific days in a month.  The months are just given standard numbers from 1-6 since the question is for a six month projection.  Plus, there is the matter of growth rate percentages to deal with.  For instance, if the minimum assumed growth rate is 7%, then I need to post 6 month sales forecasts with the added growth rate percentage.  The growth rate goes up to a maximum of 4%.  So therefore, 7% would go to 11% or 5% would go to 9%.  I need a formula that disregards specific days in a month and takes into account growth rate percentages.
OK, well, that wasn't specified, but it changes everything. :) I knew it couldn't be as simple as that.

It would help me to see the actual text of the question, if it's not too much trouble...

Dark Fact

#18
Sure, no problemo. :)

A paper products manufacturer needs a program to forecast its sales over a six-month period.  To do so requires knowing the value of the sales growth rate and how the sales grow over that period.  Write a program that asks the user to enter the minimum assumed growth rate (as a percent) and the sales for the current month.  Assume a linear growth, that is, the sales for a month equals a fixed percent (the growth rate) over the previous month.  The program should display, in a table, the projected sales for the minimum growth rate and for growth rates up to 4% greater than the minimum.  The table should look like the following, if the user enters 7 for the minimum growth rate and 30000 for the current month's sales.

Sales Projections
Current Month's Sales: 30000
Growth                                                                                   Month

Rate in %
1                2                3              4               5                6
7
8
9
10
11

NOTE: Month and growth are on the same line.  Stupid forum formatting or an optical illusion? :shock:
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Hobo Xiphas

Use the code tag for preformatted stuff

offsidewing

Quote from: guest on 06/05/2007, 12:14 AM
Quote from: offsidewing on 06/04/2007, 11:49 PMAround my office, I'm the boss.  Anyone caught posting on pcengine-fx.com during work hours (like I sometimes do) would immediately get a raise and promoted.  Sadly, no cool people work for me...
Are you hiring?  'Cause I'd be willing to relocate for some of this easy money.  :lol:
Actually, yes.  Two positions that I always snicker after reading.

Rack Coordinator.  Yes, you manage racks.

Packaging Engineer.  Help design a better package.

Snicker, Snicker.

Better have all your shit done when I catch you on pcengine-fx.com.

nat

Quote from: Dark Fact on 06/05/2007, 03:02 PMA paper products manufacturer needs a program to forecast its sales over a six-month period.  To do so requires knowing the value of the sales growth rate and how the sales grow over that period.  Write a program that asks the user to enter the minimum assumed growth rate (as a percent) and the sales for the current month.  Assume a linear growth, that is, the sales for a month equals a fixed percent (the growth rate) over the previous month.  The program should display, in a table, the projected sales for the minimum growth rate and for growth rates up to 4% greater than the minimum.  The table should look like the following, if the user enters 7 for the minimum growth rate and 30000 for the current month's sales.
OK, now I understand what they're getting at.

However, I get the impression you are looking for a single complex formula that can be used to calculate everything in that table. Since I'm no math major, I'm not sure I can help you there without spending a lot of time pondering over it. :(

I know the way *I* would do it which might be the "long way" around:

month1projection = ((currentsales)rate1  + currentsales)
month2projection = ((month1projection)rate1 + month1projection)
...
month6projection = ((month5projection)rate1 + month5projection)

then for the next tier down:

month1projectiontier2 = ((currentsales)rate2 + currentsales)
etc

and that's assuming:

currentsales = Current month's sales
rate1...rate5 = minimum growth rate...minimum growth rate+.04

It would be fairly simple for me to write a program to do what the question asks using the simple formulas here but that seems like it's got to be too easy. Plus the program would be fairly bloated. I might be able to figure it out if I spent a lot of time on it, but math is not one of my strengths, sadly. That's why you'll never find me without a calculator within arm's reach.

Dark Fact

#22
Math's not my best subject either so you're not alone. :wink:

This is part of the chapter that requires the use of for loops to write your programs. *hint* *hint*

EDIT: I took your formulas to task and made this part of the loop body:

for (month_count = MONTH_START; month_count <= MONTH_END; month_count++)
              {
                   projected_sales = (current_month_sales * (percent_count / PCT_CONVERSION) + current_month_sales);
                   current_month_sales = projected_sales;
                   printf("  %6.2lf", projected_sales);
              }
The program outputs the projected sales results in "projected_sales" and then assigns the results into "current_month_sales".  The output is displayed afterwards.  Then, when "month_count" increments, "projected_sales" will be given a new value based on the newly assigned values in "current_month_sales".  Does this work?
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Kitsunexus

I'm so great at math I had to take remedial algebra and geometry and never got higher than C's.

This has no relevance to anything. Thank you. ;)
Forgive me, my brain is on par with the Bubble System.
THE CHEMICAL BROTHERS RULE!

Nazi NecroPhile

Quote from: offsidewing on 06/05/2007, 09:48 PMBetter have all your shit done when I catch you on pcengine-fx.com. 
Fuck that noise!  I wanted to just roam pcengine-fx.com all day and keep getting raises and promotions, but you'd actually expect me to work.  I'd only take the Packaging Engineer job if I could put my package in a giant cardboard box with an expanded polystyrene insert (damn, that sounds dirty too).
Ultimate Forum Bully/Thief/Saboteur/Clone Warrior! BURN IN HELL NECROPHUCK!!!

Kitsunexus

Forgive me, my brain is on par with the Bubble System.
THE CHEMICAL BROTHERS RULE!

offsidewing

Quote from: guest on 06/06/2007, 03:29 PM
Quote from: offsidewing on 06/05/2007, 09:48 PMBetter have all your shit done when I catch you on pcengine-fx.com. 
Fuck that noise!  I wanted to just roam pcengine-fx.com all day and keep getting raises and promotions, but you'd actually expect me to work.  I'd only take the Packaging Engineer job if I could put my package in a giant cardboard box with an expanded polystyrene insert (damn, that sounds dirty too).
I'll tell you what.  I'll hire you as my admin, and you will be my pcengine-fx.com proxy poster.  Wait, no.  Cause then I'll have the ugliest admin in the building.  Hmmm...  I've got it.  I put you in logistics, but really, you'll spend your entire day keeping me apprised of all the posts in the forums.

"Hey, boss.  Nat just posted another thread saying that 'Spaceballs 3: The search for the second movie' makes for a better sequel than 'Big Trouble in Little Korea!'"

nat

Quote from: offsidewing on 06/07/2007, 05:35 PM"Hey, boss.  Nat just posted another thread saying that 'Spaceballs 3: The search for the second movie' makes for a better sequel than 'Big Trouble in Little Korea!'"
Well, it does.

Quote from: Dark Fact on 06/06/2007, 02:53 PMMath's not my best subject either so you're not alone. :wink:

This is part of the chapter that requires the use of for loops to write your programs. *hint* *hint*

EDIT: I took your formulas to task and made this part of the loop body:

for (month_count = MONTH_START; month_count <= MONTH_END; month_count++)
              {
                   projected_sales = (current_month_sales * (percent_count / PCT_CONVERSION) + current_month_sales);
                   current_month_sales = projected_sales;
                   printf("  %6.2lf", projected_sales);
              }
The program outputs the projected sales results in "projected_sales" and then assigns the results into "current_month_sales".  The output is displayed afterwards.  Then, when "month_count" increments, "projected_sales" will be given a new value based on the newly assigned values in "current_month_sales".  Does this work?
Yeah, there you go. That works. What's next?

Dark Fact

That's what I thought.  Thanks for the help, nat.  I'll be sure to post here again when I need new help. :wink:

And Offsidewing, Kitsunexus, will you mind not spamming this thread? This is supposed to be a serious discussion thread from one fellow programmer to another.
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Kitsunexus

13 posts are unrelated to the thread, in what was counted as a 29 post thread. That leaves us with 16 programming posts.


The law of the net says that in a thread hijack the predominant subject must be the one to continue on, so as you wish Dark Fact. I solemnly swear to leave this thread alone unless I have something related to programming to say.
Forgive me, my brain is on par with the Bubble System.
THE CHEMICAL BROTHERS RULE!

offsidewing

I'm hosting a sales meeting on Tuesday.  I was going to get you some advice, but now I'm not gunna.

Nya nya-nya nya nya nyaaaaa.  Yes even successful business types post on pcengine-fx.com!!!!!!

Kitsunexus

Quote from: offsidewing on 06/07/2007, 09:23 PMI'm hosting a sales meeting on Tuesday.  I was going to get you some advice, but now I'm not gunna.

Nya nya-nya nya nya nyaaaaa.  Yes even successful business types post on pcengine-fx.com!!!!!!
What exactly do you do? In like 5 days I'll be a Customer Care Representative for T-Mobile specializing in pre-paid activation. ^__^

/Sorry, I broke my promise. :(
Forgive me, my brain is on par with the Bubble System.
THE CHEMICAL BROTHERS RULE!

offsidewing

#32
I'm pretty much a corporate locust, but in a good way.  Currently, I'm employed by a Supply Chain Conglomorant and I'm contracted to a major US automaker.  I'm a complex business practice problem solver.  If there's an underperforming operating group, they plug me in to make sense of it.  I don't hire and fire, I get people out of the "if it ain't broke.." and "it's not my job" mentality.  Before that I was an investment banker.  Before that an athlete type.  Before that I was a giant man-whore in college.  Before that I was one of two people in my huge high school to have a Turbo Grafx 16.

When I'm not dodging head hunters, I waste time on pcengine-fx.com instead of finishing a few written works.

Edit:  Oh, and I don't know a thing about forum posting etiquitte.  I thought this was spamming:

"Hey, I just broke up with my boyfriend and saw your profile.  I'm into 'rounder' guys, I wonder if you'd like to meet up for drinks sometime.  Email me at vixenchick@youthinkiamreal.com."

Dark Fact

Guys...seriously, no spamming.  I don't want to have to scroll through 3 or 4 posts about nothingness just to get to my answers from nat or someone else.  You're more than welcome to make up a new thread with all your silliness but let a guy have his spot, ok?
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Kitsunexus

OMG USE THE PM SYSTEM GODDD U R SO ANNOYING AND I HAVE TOO MUCH SUGAR AND I THINK IM GONNA PUKE  :-#
Forgive me, my brain is on par with the Bubble System.
THE CHEMICAL BROTHERS RULE!

Dark Fact

PM? This thread exists not only to help me out on programming problems but for anyone else who also wants similar programming help, hence why it's called "The programming help thread".  I don't need you to tell me how to ask for help, kthnx.
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Kitsunexus

That was a joke....


Anyway, I'm starting to learn Ruby (my first language to learn other than BASIC) so I'll probably use this threa dlegit from now on. ;)
Forgive me, my brain is on par with the Bubble System.
THE CHEMICAL BROTHERS RULE!

Dark Fact

Thank you.  Now, back to business:

I've got a question that asks me to enter an integer, see if the integer is divisible by 6, and then display an appropriate message afterwards.  The coding was no problem.  The question on the other hand, is too generalized.  When it means "divisible by 6", does it mean getting a result that appears as a whole number and/or a real number?

Here's my code for the meat of the program:

for (int_count = EVEN; int_count <= int_prompt - int_prompt; int_count++)
        {
          if (int_prompt / DIVISIBILITY)
             printf("\nThe integer is divisible by 6.");
          else
             printf("\nThe integer is not divisible by 6.");
        }
DIVISIBILITY has been predefined as "6".  The result I get from the program has any number less than 6 be declared "not divisible" while any number equal to or greater than 6 declared "divisible".  Is this correct?
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

I think I'd interpret the question to mean "Is the number evenly divisible by 6?"

I just can't see it any other way.

Assuming this, I don't think your program there does the job. If I understand you correctly, your code reports 1-5 as "not divisible" while 6 and up are "divisible". That's simply not correct if we're assuming what I'm assuming. 7 is not evenly divisible by 6. Neither is 8. Or 9.

Your program is simply checking the success of the (int_prompt / DIVISIBILITY) operation.

Let's change things around a little bit and use a modulus operator:

     if ( int_prompt % DIVISIBILITY == 0 )
          printf("\nHell yeah! We're cruisin' now!");
     else
          printf("\nYour number makes Nat a sad Panda.");


My C++ skills are a bit rusty but that looks right to me.

Dark Fact

Yeah, that did the trick. :) Modulus makes programs happy. :)
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Dark Fact

New problem:

I have a program that requires me to check to see if the amount withdrawn from an account in regular cash withdrawals or checks exceeds the amount already present.  For instance, if I had $2000 in my account, deposited $1000, withdrew $3000 in cash, and another $3000 in checks, my closing balance would come to $-3000.

I have an if statement that checks to see if the closing balance falls below $0.00 and if it does, displays the amount that has exceeded.

However, for some strange reason, my program was able to display the amount a couple of times but afterwards, mysteriously stopped when I put a #define statement for "0.00".  Even when I tried to undo it, the statement still didn't appear.  Here's the code:

/* Prob 8-10
*  Created by Yuriy Mokriy
*/

#include <stdio.h>

#define ACCOUNT_LIMIT 0.00          /* The limit of the account between positive and negative balance */

main()
{
      int choice;                   /* The user's selected choice */
      float  opening_balance,       /* The opening account balance */
             total_deposit,         /* The total of all deposits */
             deposit,               /* The amount of the deposit */
             total_check,           /* The total of all checks */
             check,                 /* The amount of the check */
             total_withdrawal,      /* The total of all withdrawals */
             withdrawal,            /* The amount of the withdrawal */
             closing_balance;       /* The closing account balance */             
             
      /* Prompt the user for the opening balance */
     
      printf("Enter your opening account balance for the month: $");
      scanf("%f", &opening_balance);
     
      /* Display menu */
     
      printf("\n(D)Deposit");
      printf("\n(C)Check");
      printf("\n(W)Withdrawal");
      printf("\n(Q)Quit");
      printf("\nMake your selection: ");
      getchar();
      choice = getchar();
     
      /* Perform action based on choice */
     
      do 
      {
             if (choice == 'd' || choice == 'D')
             {
                  printf("\nEnter the amount of the deposit: $");
                  scanf("%f", &deposit);
                  total_deposit += deposit;
             }
             else if (choice == 'c' || choice == 'C')
             {
                  printf("\nEnter the amount of the check: $");
                  scanf("%f", &check);
                  total_check += check;
             }
             else if (choice == 'w' || choice == 'W')
             {
                  printf("\nEnter the amount of the withdrawal: $");
                  scanf("%f", &withdrawal);
                  total_withdrawal += withdrawal;
             }         
             else
                 printf("\nError.  You must select the following: ");
             
             printf("\n(D)Deposit");
             printf("\n(C)Check");
             printf("\n(W)Withdrawal");
             printf("\n(Q)Quit");
             printf("\nMake your selection: ");
             getchar();
             choice = getchar();
      } while (!(choice == 'q' || choice == 'Q'));
         
      /* Perform calculation */
     
      closing_balance = opening_balance + total_deposit - total_withdrawal - total_check;
     
      /* Check if account is overdrawn */
     
      if (closing_balance < ACCOUNT_LIMIT)
      {
         printf("\nWARNING: Your account has been overdrawn by $%.2f", closing_balance + ACCOUNT_LIMIT); 
         printf("\n");
      }
     
      /* Display totals */
     
      printf("\nOPENING BALANCE: $%.2f", opening_balance);
      printf("\nTOTAL DEPOSIT:   $%.2f", total_deposit);
      printf("\nTOTAL WITHDRAWAL:$%.2f", total_withdrawal);
      printf("\nTOTAL CHECKS:    $%.2f", total_check);
      printf("\n------------------------");
      printf("\nCLOSING BALANCE: $%.2f", closing_balance);
}
Can you figure out why the if statement that checks for the overdrawn amount in the account won't appear in my program? Because I'm just positively baffled here. :?

Another thing, the program instructions ask for me to display a warning at any time during the program a withdrawal or check results in a negative balance.  Does the program want me to check for an over withdrawal each time the check amount or cash withdrawal falls below $0.00 or does it have to be after the closing balance has been computed?

Take your time guys.  I think I said a mouthful. :P
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

Is your tutorial a C or C++ tutorial? Not that it makes a whole lot of difference, but #DEFINE is relatively obsolete in C++. Even for C, your usage is not entirely correct.

#DEFINE is typically used to assign macros and boolean attributes to an identifier. It looks like you are thinking of #DEFINE as a way to assign a value to a variable, which it does not.

Forget #DEFINE. I try not to use #DEFINE at all unless absolutely necessary.

What your problem is here is that you are trying to compare a floating point (closing_balance) to an identifier. It's not going to printf that statement because that comparison will never be "true".

Delete the #DEFINE line and change your variable definitions to look as follows:

       float  opening_balance,       /* The opening account balance */
             total_deposit,         /* The total of all deposits */
             deposit,               /* The amount of the deposit */
             total_check,           /* The total of all checks */
             check,                 /* The amount of the check */
             total_withdrawal,      /* The total of all withdrawals */
             withdrawal,            /* The amount of the withdrawal */
             closing_balance,       /* The closing account balance */   
             nat_rules = 0.00;      /* Clearly, Nat is the man. */     
     


And of course, change all instances of "ACCOUNT_LIMIT" to "nat_rules".

Dark Fact

Okay, the problem with the overdrawn closing amount not appearing was a faux pas on my part.  I had another program earlier that used the same format but didn't need the overdrawn amount message to appear.  I just wound up  inadvertently running that previous program instead of the current one.

However, the question regarding where the overdrawn amount message needs to be displayed still stands.  Does it need to be displayed the moment the withdrawn or check amount goes below 0.00 or when the closing balance has been tabulated?
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

I would say it needs to be displayed at the moment the balance goes negative, but that's a semantics issue.

Dark Fact

I thought as much.  I fixed the problem by creating a new variable called "account_balance" that assigns the opening balance to it as well as any deposits, withdrawals, or checks made.  I then cut and pasted the if statement that checks for overdrawn amounts into my while loop.  "account_balance" was used as a condition in the statement.  Finally, I assigned "account_balance" to "closing_balance" in order to display the closing balance amount in my program.

Thanks guys. :) I'll be back when I have another problem. :wink:
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

Quote from: Dark Fact on 06/20/2007, 10:16 PMThanks guys. :) I'll be back when I have another problem. :wink:
Fine with me. But as far as I know, I'm only one person. My name isn't Legion. :)

offsidewing

Quote from: nat on 06/20/2007, 11:41 PM
Quote from: Dark Fact on 06/20/2007, 10:16 PMThanks guys. :) I'll be back when I have another problem. :wink:
Fine with me. But as far as I know, I'm only one person. My name isn't Legion. :)
So that means you are not many?

GIVE US A KISS, DEAR!!

nat

Quote from: offsidewing on 06/25/2007, 08:26 PMSo that means you are not many?

GIVE US A KISS, DEAR!!
Be careful what you wish for.

Dark Fact

Okay, back with another problem.

I've been stewing with this program for a whole day.  The question in the book asks me to write a whole bunch of functions for an accounting program.  One of them involves getting the opening balance to an account, the other involves displaying a menu, another involves validating a user's choice, and another that displays the final results.

The problem I'm having with the program is the accumulation of the total amounts of deposits, withdrawals, and checks appearing properly.  Now, the funny thing is, when I'm typing in values for the program and finally end it afterwards, sometimes the values appear correct but other times, they don't.  For example, suppose I do the following:

Opening Balance: $40000
Deposit: $1000
Deposit: $1000
Withdrawal: $250
Withdrawal: $100
Check: $50

And then I end the program which gives me the following totals:

OPENING BALANCE: $40000
TOTAL DEPOSIT: $2000
TOTAL WITHDRAWAL: $350
TOTAL CHECKS: $50
--------------------------
CLOSING BALANCE: $41600

Now here's the same program using different amounts:

Opening Balance: $40000
Deposit: $60000
Deposit: $50000
Withdrawal: $24000
Check: $15000
Deposit: $25000

Here's what I may get:

OPENING BALANCE: $40000
TOTAL DEPOSIT: $-120000
TOTAL WITHDRAWAL: $156301606014
TOTAL CHECKS: $68500
--------------------------
CLOSING BALANCE: $1258917250218000000000000000000

See? Sometimes, good amounts, sometimes bad amounts.  Question is...what do I do to prevent any of these garbage values from appearing at all? Is it a problem with my identifiers? By the way, my identifiers for my variables are all double.
homepage2.nifty.com/tkdate/ysmusic/screen/graphic/Win_CP_THE_LAST.jpg
Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

First, if you're only dealing with whole dollars in this program change your data type for all your variables to long int, although it's probably not a big deal.

Next, without seeing your code, I'm going to guess you aren't initializing your variables. Depending how your program is set up this could make a big difference. I always believe it's good practice to initialize variables as you declare them regardless:

long int         nat_rules = 0,
                 dark_fact_rules = 0,
                 we_all_rule = 0;


Without seeing your code it's going to be tough debugging the program further.... Try the initialization thing and if that doesn't work, post up your code.