#include <iostream.h>
#include <math.h>
void centscalculation(int cents, int &count2, int &count7, int
&count9) {
int Total, diff, mindiff=999999, x, y, z, Nstamp, NewNstamp=999999;
//x is for 9cents, y is for 7cents, z is for 2cents;
for(x=cents/9; x>=0; x--) {
for(y=(cents-x*9)/7; y>=0; y--) {
z=(cents-x*9-y*7)/2;
Total=(z*2+y*7+x*9);
if (Total>=cents) {
} else {
Total=Total+2;
z=z+1;
}
diff=Total-cents;
if (diff<mindiff) {
mindiff=diff;
count2=z;
count7=y;
count9=x;
} else if (diff==mindiff) {
Nstamp=x+y+z;
NewNstamp=count2+count7+count9;
if (Nstamp<NewNstamp) {
count2=z;
count7=y;
count9=x;
}
}
}
}
}
void main () {
int cents;
int count2, count7, count9;
while (true) {
cout<<"Enter cents, 0 to terminate: "<<endl;
cin>>cents;
if (cents<0) {
cout<<"Error."<<endl;
continue;
}
if (cents==0) {
break;
}
centscalculation(cents, count2, count7, count9);
cout<<"answer is "<<count2<<" 2 cents stamps "<<count7<<" 7 cents
stamps "<<count9<<" 9 cents stamps."<<endl;
cout<<"The total number of stamps is "<<count2+count7+count9<<endl;
}
} 2 1374 da******@brentwood.bc.ca wrote:
We use for loops extensively, what are you talking about.
#include <iostream.h>
No such header in standard C++.
int Total, diff, mindiff=999999, x, y, z, Nstamp, NewNstamp=999999;
//x is for 9cents, y is for 7cents, z is for 2cents;
This isn't even good C coding style. It's retched C++. Declare in
the smallest scope they are needed and provide initializers.
for(x=cents/9; x>=0; x--) {
for(y=(cents-x*9)/7; y>=0; y--) {
A commment explaining what the hell it is you're attempting to do here
would be nice. Is y really supposed to count to zero here or just down
to cents - (x-1)*9?
What is your question anyhow?
>
void main () {
The above is illegal in C and C++.
>
int cents;
int count2, count7, count9;
while (true) {
cout<<"Enter cents, 0 to terminate: "<<endl;
cin>>cents;
You should test to see if this fails.
>
On Thu, 08 Mar 2007 06:53:38 -0800, davy.zou wrote:
Hey! I use for loops!
[snip some code with for loops]
Wow! so do you!
(did I miss the point?)
--
Lionel B This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Oleg Leschov |
last post by:
Could there be means of exiting nested loops in python?
something similar to labelled loops in perl..
I consider it irrating to have to make a flag for sole
purpose of checking it after loop if...
|
by: Dr. David Kirkby |
last post by:
I have a program that loops through and changes all the elements on an
array n times, so my code looks like this:
for (n=1; n < n_max; ++n)
for(i=imax; i >= 0; --i) {
for(j=0 ; j < jmax; ++j) {...
|
by: Neptune |
last post by:
Hello.
I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)"
(Sam's series), and for nested loops, he writes (p116) "It's often
necessary to create a loop even when you are...
|
by: Scott Brady Drummonds |
last post by:
Hi, everyone,
I was in a code review a couple of days ago and noticed one of my coworkers
never used for() loops. Instead, he would use while() loops such as the
following:
i = 0;
while (i...
|
by: John Salerno |
last post by:
I'm reading Text Processing in Python right now and I came across a
comment that is helping me to see for loops in a new light. I think
because I'm used to the C-style for loop where you create a...
|
by: Putty |
last post by:
In C and C++ and Java, the 'for' statement is a shortcut to make very
concise loops. In python, 'for' iterates over elements in a sequence.
Is there a way to do this in python that's more concise...
|
by: bitong |
last post by:
I'm a little bit confuse with regard to our subject in C..We are now with the Loops..and I was just wondering if given a problem, can you use Do-while loops instead of a for loops or vise versa? are...
|
by: monomaniac21 |
last post by:
hi all
i have a script that retrieves rows from a single table, rows are
related to eachother and are retrieved by doing a series of while loops
within while loops. bcos each row contains a text...
|
by: Nathan Sokalski |
last post by:
I have several nested For loops, as follows:
For a As Integer = 0 To 255
For b As Integer = 0 To 255
For c As Integer = 0 To 255
If <Boolean ExpressionThen <My CodeElse Exit For
Next
If Not...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |