Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,057 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,576 people online right now. Registration is fast and FREE... Join Now!




Conditional Loop

 
Reply to this topicStart new topic

Conditional Loop

Rating  5
yongone
19 Jul, 2008 - 09:00 PM
Post #1

New D.I.C Head
*

Joined: 7 Jul, 2008
Posts: 5

cpp

#include <stdio.h>

int
main()
{
int x[50];
int i;

for(i=0; i<50; i++){
x[i] = i * 3;
}

for(i=0; i<50; i++){
printf("%d", x[i]);
if (i = 10 || 20 || 30 || 40)
printf("\n");
}
return(0);
}



OK!
Ive been trying to figure this out for 3 hours now..

I know there is something wrong with the if statement but i cant figure it out.
If there is another way to do this it would be great.

Make a program with an array of fifty integers. Use a loop to set the values of the array to the multiples of three, starting at zero. Use another loop to print the array, but with two differences. Use a conditional inside the loop to insert a newline after every tenth number, and use formatting information to line up the numbers in columns as shown:

CODE

2.           0   3   6   9  12  15  18  21  24  27

3.          30  33  36  39  42  45  48  51  54  57

4.          60  63  66  69  72  75  78  81  84  87

5.          90  93  96  99 102 105 108 111 114 117

6.         120 123 126 129 132 135 138 141 144 147

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Conditional Loop
19 Jul, 2008 - 09:39 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,198



Thanked: 212 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Here is something for you to play with and look at. Read the in code comments to see what I am doing and how I accomplished various tasks.

cpp

#include <stdio.h>

int main()
{
int x[50];
int i;

for(i=0; i<50; i++){
x[i] = i * 3;
}

// Counter is for the row number
int counter = 1;

// Loop through the array
for(i=0; i<50; i++){
// If "i" is a multiple of 10, write a new line, display line number
// and increment that number for the next line
if ((i % 10) == 0) {
printf("\n");
printf("%d.",counter);
counter++;
}

// Print the number using a width of 4
printf("%4d", x[i]);
}

printf("\n\n");
return 0;
}


Enjoy and go watch some tv or something. wink2.gif

"At DIC we be row printing code ninjas....and some of us have been around since the invention of movable type" decap.gif
User is online!Profile CardPM
+Quote Post

NickDMax
RE: Conditional Loop
20 Jul, 2008 - 09:09 AM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Just a note here.

The i % 10 is the way to go here but for future referance:

if (i = 10 || 20 || 30 || 40) { } This will not work well.

This translates to:
if (true || true || true || true)

Hardly, I think, what you wanted to achieve.

The reason is this. I C/C++ a conditional expression is evaluated as 0 == false, and non-zero == true.

So you first condition (a very common error in programming in general) is "i = 10" which is an assignment of the value 10 into the variable i. Therefore the overall value of this expression is the ending value of the left and side: 10. And 10 is non-zero and therefore true.

You next conditional expression is 20, which is of course true as well, and so on.

SO, the over all conditional expression of "true || true || true || true" is of course "true".

So, how would you expression have looked if written out properly?

if (i == 10 || i == 20 || i == 30 || i == 40)

which is not exactly the same as:

if ((i % 10) == 0) which incidentally is the same as if (!(i % 10)) (think about it).

This works by using the modulus operator which finds the remainder of the division: i % 10 == Remainder( i / 10 ) == the 1's place.

I hope this helps you understand what what wrong with your program above.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 06:02PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month