Thread: Stuck on a friend's challange

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void box(int n) {
       int j = -1, k = -1;
       for (int i = 0; i < n * n; ++i) {
          if (j++ == n - 1) {
             k = -1;
             j = 0;
             cout << endl;
          } else if (j == i / n) {
             k = 0;
          } else if (j > i / n) {
             k = 1;
          }
          cout << setw(2) << k << ' ';
       }
    }
    
    int main() {
       int n;
       cout << "sides = " << flush;
       cin >> n;
    
       box(n);
       return 0;
    }
    Prints:
    Code:
    sides = 5
     0  1  1  1  1
    -1  0  1  1  1
    -1 -1  0  1  1
    -1 -1 -1  0  1
    -1 -1 -1 -1  0
    Except its the other way around! One loop.

    EDIT: Darn I wanted to not give him the correct code so I did it the opposite way. Oh well. Its an easy fix to make it the other way.
    Last edited by Speedy5; 04-21-2004 at 04:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM