#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    1
    Rep Power
    0

    Question trouble with loops


    Needing to code these two little programs i find myself lost completely as to how exactly to do them any pointers would be greatly appreciated. These are questions from a learn C in 21 days book as im trying to learn C in my spare time to help me in my Job field and i just dont get how to do these altho im probably overlooking something stupidly simple. Anyways any help would be most appreciated as ive looked all over the dang book and all over google. :confused: One would think the stupid book would give the answers but it doesnt =(



    Write a program that writes your last name backwards on the monitor and your age (21.75) three times with extra indentation in each line. Your age should show increasing precision fron line to line. Write this program three times, once with each looping method.

    /* Results of execution
    DRANREB 21.8
    DRANREB 21.75
    DRANREB 21.750
    */




    Write a program that counts from 229 to 220, prints three numbers on a separate line, and includes a message like below when the line has an even multiple of 5 and a different message when the count is 225. This program must employ a loop of your choice to print these lines.

    /* Results of execution
    The numbers are now 229 228 227.
    The numbers are now 226 225 224 with an even multiple of 5.
    The numbers are now 223 222 221.
    The number is now 220 with an even multiple of 5.
    */
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Austin TX yall
    Posts
    42
    Rep Power
    12
    The best way to get help here is not to ask for the answer to a programming assignment as I've figured out. I'd more than glad to answer a specific question about your problem but do not have the time to do the work for you. Your best bet to get some help is to show us what you have done and what problems you are encountering. the best way to learn although for me is very slow is just to sit there and hack it out. Work out the problem or tell me what you're having problems with and i'll be glad to help. =)
    Thanks,

    Jonathan Cobb
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Austin TX yall
    Posts
    42
    Rep Power
    12
    Code:
    int main () {
    
    	int linewidth =15;  //initialize linewidth to take the first however many characters you need
    	for (int i = 0; i<3;i++){
    		cout << setw (linewidth);
    		cout << "string" << endl;
    		linewidth +=5;  //add five to the width of the line acts as an indent
    	} // end for loop
      return 0;
    }
    also a good reference on the iomanip.h lib that you should read up on is located at http://www.cplusplus.com/ref/iostream/iomanip/

    good luck.
    Thanks,

    Jonathan Cobb
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Austin TX yall
    Posts
    42
    Rep Power
    12
    An answer, without documentation i might add, could look like this:

    Code:
    // setw example
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main () {
    	int max = 229;
    	int min = 210;
    	int count =0;
    	bool mult= false;
    
    	for (int i= max; i>=min; i--){
    		if (count%3 == 0){
    			cout << endl << "The numbers are now " ;
    		}	
    		count ++;
    		if (i%5 == 0){
    			mult=true;
    		}
    		cout<< i << " " ;
    		if( mult== true){
    			if (count%3==0|| i==min){
    			cout << "with an even multiple of 5.";
    			mult = false;
    			}
    		}
    		
    	}
    
    	
      return 0;
    }
    /*
    Write a program that counts from 229 to 220, prints three numbers on a 
    separate line, and includes a message like below when the line has an even multiple of
     5 and a different message when the count is 225. This program must employ a loop of
     your choice to print these lines. 
    
    /* Results of execution 
    The numbers are now 229 228 227. 
    The numbers are now 226 225 224 with an even multiple of 5. 
    The numbers are now 223 222 221. 
    The number is now 220 with an even multiple of 5. 
    */
    Why can't my data structures professor give me assignments like this? :confused:
    Thanks,

    Jonathan Cobb
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    JonnyFive, he's learning C, not C++. Even though the actual for-loop part of your examples also work in C, the iostream I/O is a confusion factor that I don't think he needs.

    Clay, take a stab at the problems. After you resolve all the syntax errors that you can (even we grizzled old programmers still get those) and have resolved all the operational problems that you can on your own, then come to us with fairly/relatively specific problems/questions and the pertinent code. That we can help you with. Besides, if we just do your problems for you, then you won't learn and your stated goal is to learn this stuff -- I'm a firm believer in teaching a man to fish rather than just handing him a fish and I've always approached tutoring from that perspective.

    Oh, and when you are ready to post code, be sure to use code tags to retain the indentation. Put [ code ] before the code and [ /code ] after it, but do not include the spaces inside the brackets (I had to include them to make the tags visible). Failure to do so will cause you to be admonished and repeated failure could make us reluctant to help you.

    To start with, do you understand for-loops? Consider these examples:
    Code:
    int i, j;
    char temp;
    char s[100];
    
    // step 100 times (0 to 99)
    for (i=0; i<100; i++)
    {
        // insert code you're iterating on 
    }
    
    // step 100 times (0 to 99) or until char is '='
    for (i=0; i<100 && s[i] != '='; i++);
    if (i == 100)
    {
        // do "character not found" processing
    }
    else
    {
        // i is the position of the '=' character , so process it
    }
    
    // start from the end of a string and work to the front
    for (i=strlen(s)-1; i >= 0; i--)
    {
        // process the i'th character
    }
    
    // a nested example
    for (i=0; i < 10; i++)
        for (j=0; j < 100; j++)
        {
            // for each i, process all the j's
        }
    
    // another nested example, useful for some sorts
    for (i=0; i < 9; i++)
        for (j=i+1; j < 10; j++)
        {
            if (s[i] > s[j])
            {
                // swap characters
                temp = s[i];
                s[i] = s[j];
                s[j] = temp;
            }
        }
    These examples should give you some ideas in working those problems.
    Last edited by dwise1_aol; October 7th, 2003 at 03:14 PM.

IMN logo majestic logo threadwatch logo seochat tools logo