![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2006
Posts: 14
Rep Power: 0
![]() |
Need Help With A Program
Hi, I'm testing a math problem for school,and I have a problem with the program I wrote to test it. Here is the [math] problem:
Joe has a deck of 100 cards. Each one has one yellow side, and one red side and each one is numbered from 1 to 100. All cards start with yellow facing up. He then turns around any cards that have a number divisible by 2. He does this for all numbers up to 100. How many cards have their yellow faces up at the end of this process? Here is the code: #include <iostream>
#include <string>
using namespace std;
const int decksize = 100;
int main()
{
string cards[decksize] = "Yellow";
int divby;
for (divby == 2; divby < decksize; divby++)
{
int i;
for (i = 0; i < decksize; i++)
{
if (cards[i] == "Yellow" && (i + 100) % divby == 0)
{
cards[i] = "Red";
}
else if (cards[i] == "Red" && (i + 100) % divby == 0)
{
cards[i] == "Yellow";
}
}
}
int yellow;
int red;
int n;
for (n = 0; n < decksize; n++)
{
if (cards[n] == "Red")
red++;
else if (cards[n] == "Yellow")
yellow++;
}
cout << "Yellow: " << yellow << endl;
cout << "Red: " << red << endl;
cin.get();
cin.get();
return 0;
}if (cards[i] == "Yellow" && (i + 100) % divby == 0) |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
your definition of cards is inaccurate. You define it as a 100 element array of strings, and then you initialize it to one string.
What's the error you're getting? |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2006
Posts: 20
Rep Power: 0
![]() |
int divby; for (divby == 2; divby < decksize; divby++) |
|
|
|
|
|
#4 | |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Quote:
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
man InfoGeek, my company sure can use a man like you. Can you compute the resulting balance of 3,500,000 policies after 6,000,000+ transactions a day? Let me know so we can return the mainframes back to IBM
![]() |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Apr 2006
Posts: 14
Rep Power: 0
![]() |
OpenLoop:
Doesn't that make all elements of the array strings? Thank you, bench, but the program is really messed up now, it returns the results: Yellow: 121 Red: -2056523657 Infogeek: I'm eventually going to make the program so the user can enter in the amount of cards and how high 'divby' goes. Also, I'm going to make it so there can be more then 2 colord on each of the cards. Maybe I'll make them pyramids. ![]() |
|
|
|
|
|
#7 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
string cards[10] <== this defines ten consecutive strings
char cards[10] <== this here is ten consecutive characters that can be called a string string card <== this is one string now, in your case, you're comparing cards[i] for a string. My guess is, you want an array where each element is a string. That would be the first case above. EDIT: maybe that's what you need: string cards[] = {"Yellow","Red","Green","Black"}; |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Apr 2006
Posts: 14
Rep Power: 0
![]() |
OpenLoop:
Didn't I already do that with: string cards[decksize] = "Yellow"; Maybe I need to add curly braces around "Yellow"? |
|
|
|
|
|
#9 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
int yellow;
int red;
int n;
for (n = 0; n < decksize; n++)
{
if (cards[n] == "Red")
red++;
else if (cards[n] == "Yellow")
yellow++;
} |
|
|
|
|
|
#10 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
Quote:
Try something like: string cards[decksize]; for (int i = 0; i < decksize; i++) cards[i] = "Yellow"; |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|