Write the nested loop that causes this output below: *It can only use one if statement ( I'm having issues solving it I never solved a nested loop like this one before seriously need some input on how to solve this program) Here is a link the proper image of the problem: https://drive.google.com/file/d/0B4ABEeUJeFbXNmZDNjh6WWRHS0V1SUlRQmwzaFh5RXpzSGk4/view?usp=sharing

1
1 2
2 3
2 3 4
2 3
1 2
1

What have you done, besides posting your homework assignment?Show your code to us and pinpoint the errors you have.We will be more than happy to help. :)

I'm trying to create a little bit of code to print this, but I have to say that the question itself is really odd.

The way in which the values change from line to line changes in an inconsistent way; Add a digit for the 2nd output, then add one to both values in the previous string, then add a digit.

It's something very awkward to achieve with loops without using a set of pre-determined, hard-coded values, which is not something that's elegant as a solution (and not something that's generally

Does the question allow you to use hard coded values?
Does the question allow us to use for loops (which have a kind of if statement, within the loop).

@humorousone - I agree - I couldn't spot an easy algorithm and even had 2 minutes trying to code it and its not easy to code it in an elegant way - a bit of brute force is needed as far as I can tell!

A continuation would help in identifying a pattern. Presumably, it might look something like this:

``````1
1 2
2 3
2 3 4
3 4 5
3 4 5 6
4 5 6 7
4 5 6 7 8
4 5 6 7
3 4 5 6
3 4 5
2 3 4
2 3
1 2
1
``````

But confirmation of that would be helpful. Otherwise this looks like a fun problem.

this is what I worked out so far I do not know if this is correct
M = 4,N= 4
loop x from 0 to N
loop y from 1 to M
print x + y + space
print newline
loop x from N-1 to 0
loop y from M to 1
print x + y + space
print newline

would this work im still trying to figure which type of loop will work I know I dont have to use if statment or am I missing something

Maybe some actual C# code would be easier to judge, hehe

:P

Are you entireley sure that the sequence isn't supposed to look like this?

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Your sequence seems a little bit different from most programming books.

I am pretty sure it supposed to look like this its in programming book

@overwraith - I think you can do that without an if statement - in the actual requirements we have numbers going up and down hence the if statement maybe?

Why did you put @overwraith? This isn't my thread?

So, whether the start value increments in the loop seems to hinge on whether the current line number is even or odd, so you get the pattern 1, 1, 2, 2, 3, 3, 4, 4, then it decrements 4, 3, 3, 2, 2, 1, 1. My code is going to look different from yours, I am trying to figure out a good way to get the even/odd thing working. The following only does the pattern I showed earlier, it doesn't increment the start value yet.

``````/*Author: overwraith*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NestedLoopExample {
class Program {
static void Main(string[] args) {

PrettyLoop(1, 9);

Console.Write("Press any key to continue... ");
}//end main

public delegate bool ComparisonBool<T>(T x, T y) where T : struct;
public delegate void IncrementDecrement<T>(ref T x) where T : struct;

public static void PrettyLoop(int start, int end) {
if (start >= end)
throw new ArgumentException("Start should be greater than end. ");

ComparisonBool<int>[] comparisons = new ComparisonBool<int>[2]{
(int x, int y) => { return x < y; },
(int x, int y) => { return x >= y; }
};

IncrementDecrement<int>[] incDec = new IncrementDecrement<int>[2]{
(ref int x) => x++,
(ref int x) => x--
};

//basically start with one level of indirection, loop alters these values
int startTmp = start;
int endTmp = end;

//int fudge = 0;

//two operations we want to switch between
for (int i = 0; i < comparisons.Length; i++) {

for (; comparisons[i].Invoke(startTmp, endTmp); incDec[i].Invoke(ref startTmp)) {

//print nums start to finish on one line
for (int j = start; j <= startTmp; j++) {
Console.Write(j + " ");
}//end loop

Console.WriteLine();
}//end loop

//Swapping operation
endTmp = start;
}//end loop

}//end method

}//end class
}//end namespace
``````

This is what I came up with but it seems not be working right:

int numColums = 1;
for(int i = 0;i<7;i++)
for(int i=0;j< numColums;j++)
Console.Writeline(j+1);

Console.Writeline();

if(1%2 ==0)

numColums;

Ok, so I figured out that in my code I had to take into account the fact that when you go half way through the numbers, the logic for the start value flips from even nums to odd nums. The code I wrote to solve this problem is deplorable, I would probably go a different route if I had to do so again. My code also takes into account start and end values for the table. The fudge operations I coded, especially the second one is really messy. I anticipate this code breaking when you plug in different values (possibly, I am not sure). I did get it to display the original table though;

``````1
1 2
2 3
2 3 4
3 4 5
3 4 5 6
4 5 6 7
4 5 6 7 8
4 5 6 7
3 4 5 6
3 4 5
2 3 4
2 3
1 2
1
``````

And here is what the table looks like for 1 through 9, am not sure if is correct;

``````1
1 2
2 3
2 3 4
3 4 5
3 4 5 6
4 5 6 7
4 5 6 7 8
5 6 7 8 9
4 5 6 7 8
4 5 6 7
3 4 5 6
3 4 5
2 3 4
2 3
1 2
1
``````

As you can see the 8's and 9's are all in essentially the same column, so I am not sure if this is a correct implementation. So apparently I had to use a terinary operator once to determine the value to increment depending on whether the line num is even or odd, then the logic flips half way through the program. Then in a really complex terinary operation I had to also account for the difference between the start and end values being even or odd. Not sure if this code will help you or not, if you find a simpler means go for it.

``````/*Author: overwraith*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NestedLoopExample {
class Program {
static void Main(string[] args) {

PrettyLoop(1, 9);

Console.Write("Press any key to continue... ");
}//end main

public delegate bool ComparisonBool<T>(T x, T y) where T : struct;
public delegate void IncrementDecrement<T>(ref T x) where T : struct;
public delegate void FudgeOperations<T>(ref T x, T y) where T : struct;

public static void PrettyLoop(int start, int end) {
if (start >= end)
throw new ArgumentException("Start should be greater than end. ");

ComparisonBool<int>[] comparisons = new ComparisonBool<int>[2]{
(int x, int y) => { return x < y; },
(int x, int y) => { return x >= y; }
};

IncrementDecrement<int>[] incDec = new IncrementDecrement<int>[2]{
(ref int x) => x++,
(ref int x) => x--
};

FudgeOperations<int>[] fudgeOperations = new FudgeOperations<int>[2]{
(ref int fudge_1, int lineNum_1) => fudge_1 += ( lineNum_1 % 2 != 0 ? 1 : 0 ),
(ref int fudge_1, int lineNum_1) => fudge_1 -= ( (lineNum_1 + ((end - start + 1) % 2 == 0 ? 1 : 0)) % 2 == 0 ? 1 : 0 )
};

//basically start with one level of indirection, loop alters these values
int startTmp = start;
int endTmp = end;

int fudge = 0;

//two operations we want to switch between
for (int i = 0; i < comparisons.Length; i++) {

for (int lineNum = 1; comparisons[i].Invoke(startTmp, endTmp);
incDec[i].Invoke(ref startTmp), lineNum++, fudgeOperations[i].Invoke(ref fudge, lineNum)) {

//print nums start to finish on one line
for (int j = start + fudge; j <= startTmp; j++) {
Console.Write(j + " ");

}//end loop

Console.WriteLine();
}//end loop

//Swapping operation
endTmp = start;
}//end loop

}//end method

}//end class
}//end namespace
``````
commented: Nice code. +15

P.S. whoever came up with this code requirement is evil, don't trust them.

My proffesor who has a ph.d in computer came up with this challange to see we can crack it so far professor 1 and students 0

So when reading my code, take into account that I am essentially using delegates (callback functions) to make one loop act like two. I have a bachelor's degree. I am inherently more trustworthy.