473,320 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how come no one here uses for loops?

#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;
}
}

Mar 8 '07 #1
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.
>
Mar 8 '07 #2
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
Mar 8 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
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...
4
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) {...
46
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...
6
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...
17
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...
10
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...
2
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...
3
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...
8
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...
0
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
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...
0
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...
0
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...
0
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...
0
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...
0
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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.