Jump to content


Photo

Need help!


  • Please log in to reply
17 replies to this topic

#1 DeeZeL

DeeZeL
  • New Members
  • 5 posts

Posted 10 April 2004 - 12:37 PM

ok, I have to make this program that uses nested loops to produce the following output:

2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25

Seems simple huh?. Heres the sorce code showing the logic I followed (I didnt do any nested loops btw):

#include <iostream> 
#include <iomanip> 
using namespace std; 
void main() 
{ 
   int a,b,c,d; 
   for (a=1; a < 11; a++) 
   { 
      a=a+1; 
      cout<<setw(2)<<a<<setw(4); 
   } 
   cout<<endl; 
   for (b=1; b < 16; b++) 
   { 
      b=b+2; 
      cout<<setw(2)<<b<<setw(4); 
   } 
   cout<<endl; 
   for (c=1; c < 21; c++) 
   { 
      c=c+3; 
      cout<<setw(2)<<c<<setw(4); 
   } 
   cout<<endl; 
   for (d=1; d < 26; d++) 
   { 
      d=d+4; 
      cout<<setw(2)<<d<<setw(4); 
   } 
   cout<<endl; 
}


now, here the output:

2 4 6 810
3 6 91215
4 8121620
510152025
Press any key to continue


Notice how there is no space between 2-digit numbers. What could cause this? help would be really appreciated.

#2 Detail

Detail

    King Detail

  • Hosted
  • 7,767 posts
  • Location:Dayonic
  • Projects:Dayvi.com
  •  Blu Spy

Posted 10 April 2004 - 01:43 PM

Thats the power of Array ;)2
You need to put a space in.

#3 Mastermind

Mastermind

    Server Technician

  • Undead
  • 7,014 posts
  • Location:Cambridge, MA
  • Projects:MasterNews 3
  •  The Man Behind the Curtain

Posted 11 April 2004 - 11:34 PM

for ( i=2; i<6; i++)
{
     for (j=1; j<6; j++)
     {
          cout<<i*j;
          cout<<" ";
     }
     cout<<endl;
}
There's nested loops that should do exactly what you need. Just declare the variables before it, and you're done.
:lol:
Posted Image

Well, when it comes to writing an expository essay about counter-insurgent tactics, I'm of the old school. First you tell them how you're going to kill them. Then you kill them. Then you tell them how you just killed them.

Too cute! | Server Status: If you can read this, it's up |

#4 DeeZeL

DeeZeL
  • New Members
  • 5 posts

Posted 01 May 2004 - 12:55 PM

Yeah, sorry for not replying here for a long time but I sorted it out, thanks for the help anyways ;)

#5 Nicolas

Nicolas
  • New Members
  • 8 posts

Posted 05 May 2004 - 01:04 AM

Help me out, I justed got a new compilter in Dev version 5.0, but it don't has a apstring class, so I can't uses it. Can you show me how to include apstring to my compiler, if you can, show me where I can get apstring codes

Also I have another problem, my teacher just come up with new program : Convert a binary number to decimal, I just learned C++, most of the basics stuff like loops and etc.... I know it suppose to use an array to convert, but I haven't learn an array yet! I know there is a way to convert without using array, just a plain <iostream.h> header filer, without anything more. I know there are a ways because my friends they done there program with a regullar variable, nothing special to it. I can't figure out how they did it.

#6 the_kid

the_kid

    L337 Pwnerer of N00bs

  • Project Team
  • 1,734 posts
  • Location:La$ Vega$, NV
  • Projects:freelancing between what interests me
  •  + TRIPLEhelix

Posted 05 May 2004 - 05:37 AM

<offtopic=sorta>In the future you may want to start a new thread for new questions</offtopic>

#7 Nicolas

Nicolas
  • New Members
  • 8 posts

Posted 05 May 2004 - 10:08 PM

Help me out, I justed got a new compilter in Dev version 5.0, but it don't has a apstring class, so I can't uses it. Can you show me how to include apstring to my compiler, if you can, show me where I can get apstring codes

Also I have another problem, my teacher just come up with new program :  Convert a binary number to decimal,  I just learned C++, most of the basics stuff like loops and etc.... I know it suppose to use an array to convert, but I haven't learn an array yet!  I know there is a way to convert without using array, just a plain <iostream.h> header filer, without anything more.  I know there are a ways because my friends they done there program with a regullar variable, nothing special to it.  I can't figure out how they did it.

I know it sorta out of topic, but I really need help so I thought......continues

#8 Detail

Detail

    King Detail

  • Hosted
  • 7,767 posts
  • Location:Dayonic
  • Projects:Dayvi.com
  •  Blu Spy

Posted 05 May 2004 - 10:24 PM

Have you asked at the Dev forums. As thay made it, thay would be the best people to help you.

#9 Nicolas

Nicolas
  • New Members
  • 8 posts

Posted 10 May 2004 - 11:48 PM

Well, I have figure out the program, it tooks me heck of a time to finish that program without our using the power of array, thank you for helping me out.

I still have trouble on how to add an apstring to the dev compiler

#10 TGMTE

TGMTE
  • Members
  • 43 posts

Posted 11 May 2004 - 08:34 PM

Using string comparision for this seems like a waste of time.

To convert binary into decimal is very simple and can be done as shown below:

Say we want to convert the 8 bit value 10011101 into a decimal value, we can use a formula like that below:

128 64 32 16 8 4 2 1
1 0 0 1 1 1 0 1

As you can see we have placed the numbers 1, 2, 4, 8, 16, 32, 64, 128 (powers of two) in reverse numerical order and then written the binary value below, to convert you simply take a value from the top row wherever there is a 1 below and add the values together, for instance in our example we would have 128 + 16 + 8 + 4 + 1 = 157. For a 16 bit value you would use the decimal values 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 (powers of two) for the conversion.

Because we know binary is base 2 then the above could be written as:

1*27 + 0*26 + 0*25 + 1*24 + 1*23 + 1*22 + 0*21 + 1*20 = 157.

#11 Nicolas

Nicolas
  • New Members
  • 8 posts

Posted 11 May 2004 - 09:47 PM

Thanks for solving, but it were just a starter of program process, it took me quite few hours, and hint from a friend for me able to figure it out. I use modulus and value 127, you can only enter 7 digits, so it's 128 / value, that value is assign to 128. It time it go by using loop, that value divide by half, I did mention I was using modulus: (128 / value) * modulus, well the rest of the program you can figure out, it like 4 or 5 lines to the program. My friend the one that gives me the hint, he did like 3 line to convert.

We got like 7 program to figure out include the binary above, I only know how to solve 5 program, the last 2 is extreme diffifult...

#12 TGMTE

TGMTE
  • Members
  • 43 posts

Posted 12 May 2004 - 03:32 PM

Care to post them?

#13 Nicolas

Nicolas
  • New Members
  • 8 posts

Posted 13 May 2004 - 02:40 PM

The last two is this:

Converts standard Arabic numbers to Roman numerals. Maximum 3000.

Extend the program to convert from Roman numerals to standard Arabic Numbers.

#14 Guest_Guest_*

Guest_Guest_*
  • Guests

Posted 13 May 2004 - 04:04 PM

This code should convert roman numerals to normal decimals.

char *ArabicNumeral(short arab) {
    short numls[7];
    short levels[7] = {1000,500,100,50,10,5,1};
    char clevel[7] = {'M','D','C','L','X','V','I'};
    char *numeral = new char[16], buf[5];
    int cl, ll, backnum = arab;
    
    memset(numeral, 0, 16);

    if (backnum > 3999)
        backnum = 3999;

    for(int cc=0;cc<7;cc++) {
        for(cl=0;cl<7;cl++) {
            if (backnum == levels[cl]) {
                sprintf(buf, "%c", clevel[cl]);
                strcat(numeral, buf);
                return numeral;
            }
        }
        
        for(cl=0;cl<7;cl++) {
            for(ll=6;ll>=0;ll--) {
                if (levels[cl]-levels[ll] == backnum) {
                    sprintf(buf, "%c%c", clevel[ll], clevel[cl]);
                    strcat(numeral, buf);
                    return numeral;
                }
            }
        }
        
        numls[cc] = backnum/levels[cc];
        backnum -= levels[cc]*numls[cc];

        if (numls[cc] >= 4) {
            sprintf(buf, "%c%c", clevel[cc], clevel[cc-1]);
            strcat(numeral, buf);
        } else {
            for(ll=0;ll < numls[cc];ll++) {
                sprintf(buf, "%c", clevel[cc]);
                strcat(numeral, buf);
            }
        }
        
        if (backnum <= 0)
            break;
    }

    return numeral;
}


#15 TGMTE

TGMTE
  • Members
  • 43 posts

Posted 13 May 2004 - 04:06 PM

Sorry, forgot to log in again.

#16 Nicolas

Nicolas
  • New Members
  • 8 posts

Posted 13 May 2004 - 06:17 PM

Hey , all you kidding, that is awsome. Well, I can't use your coding to turn in anyway, I like to figure out by myself, that what program interesting right, it a challenge that you want to catch.

Can you extend the program and made it a reverse.

Well that program is to complex man, We haven't even study function and arry yet, but I like to study by myself ahead and basic understanding. Even thought , I got C++ book at home.

Here is a program that I made guess a number between 1 and 100, user thinks of a number and I would able to guess that number by asking high or low, and just keep asking the user.

Here is what I make:

Attached Files



#17 Nicolas

Nicolas
  • New Members
  • 8 posts

Posted 13 May 2004 - 10:46 PM

Hey, that awsome, but I can't coppy the code and turn in, there were no challenge in that, that what program is, it the challenge that we are seeking right.

Thanks, I can use your program to study and hopful I can figure it out. I haven't study function and arry yet, but I like to study by my own and I guess I can figure our your program, hopfully <_< .

Well, here is another program that I make that ask the user thinks of a number and the program will guess what that number is. The number between 1 and 100.
1st guess should be 50, then ask is it higher or lower....if it was wrong.

Here the code:

[code=auto:0]

Attached Files



#18 Nicolas

Nicolas
  • New Members
  • 8 posts

Posted 13 May 2004 - 10:48 PM

Oops, sorry that I post it 2 time in a day, I thought I lost the first post, but again I didn't see there is a second page. :(




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users