nested-loops

Factorial using Addition

僤鯓⒐⒋嵵緔 提交于 2019-12-20 05:53:35
问题 I am attempting to create a C code that finds the factorial of a integer so that I may convert my code to assembly language. My code seems to 'multiply' the second integer twice. i.e. 5*4*4*3... I cannot seem to find out why. Help please! #define N 5 int main() { int j = 0; int i = 0; int num1 = N; int num2 = N - 1; int sum = 0; while (num2 != 0) { while (j < num2) { sum += num1; j++; } j = 0; printf("%d\n", sum); printf("--------------\n"); --num2; num1 = sum; } printf("--->%d", sum); }

Nested loops result

被刻印的时光 ゝ 提交于 2019-12-20 04:25:08
问题 I really don't know how to find out the result of nested loops. For example in the following pseudo-code, I can't sort out what will be given at the end of execution. I'll be so glad if anyone gives me a simple solution. r <- 0 for i <- 1 to n do for j <- 1 to i do for k <- j to i+j do r <- r + 1 return r Question is: What is the result of the code and give the result r in terms of n ? I write it but every time I get confused. 回答1: In your pseudo-code, Inner most loop, k <- j to i+j can be

Nested loops for creating a spiral shape pattern in c

ぐ巨炮叔叔 提交于 2019-12-19 10:19:45
问题 I need to make a spiral pattern made of stars '*' using nested for loops. I managed to make outter lines, now I don't know how to repeat smaller swirls in the same place. What I should have: ********* * ******* * * * * * *** * * * * * * * ***** * * * ********* Any help would be greatly appreciated. 回答1: After being thoroughly nerd-sniped, I came up with this: #include <stdio.h> void print_spiral(int size) { for (int y = 0; y < size; ++y) { for (int x = 0; x < size; ++x) { // reflect (x, y) to

Reuse nested loops without copy and paste

南楼画角 提交于 2019-12-18 16:38:09
问题 Suppose I've this nested loop for (int a=1; a<MAX_A; ++a) for (int b=1; b<MAX_B; ++b) for (int c=1; c<MAX_C; ++c) { do_something(a, b ,c); } and I reuse this loop in various part of my code, changing the function do_something . It's quite boring to rewrite every time the first three lines. In python for example I would created a generator to return an iterator (1, 1, 1), (1, 1, 2), ... or something like itertools.product . In c++ the only solution I've in mind is to define a macro. Something

Is this a valid (ab)use of lambda expressions?

旧城冷巷雨未停 提交于 2019-12-18 13:07:33
问题 Like we all know, it's not that easy to break from a nested loop out of an outer loop without either: a goto (Example code.) another condition check in the outer loop (Example code.) putting both loops in an extra function and returning instead of break ing (Example code.) Though, you gotta admit, all of those are kinda clumsy. Especially the function version lacks because of the missing context where the loops are called, as you'd need to pass everything you need in the loops as parameters.

Efficiency of nested Loop

会有一股神秘感。 提交于 2019-12-18 06:01:27
问题 See the following snippet: Long first_begin = System.currentTimeMillis(); // first nested loops for (int i = 0; i < 10; i++) { for (int j = 0; j < 1000000; j++) { // do some stuff } } System.out.println(System.currentTimeMillis() - first_begin); // second nested loops Long seconde_begin = System.currentTimeMillis(); for (int i = 0; i < 1000000; i++) { for (int j = 0; j < 10; j++) { // do some stuff } } System.out.println(System.currentTimeMillis() - seconde_begin); I am wondering why the

Is it possible to exit a for before time in C++, if an ending condition is reached?

和自甴很熟 提交于 2019-12-18 02:58:14
问题 I want to know if it is possible to end a for loop in C++ when an ending condition (different from the reacheing right number of iterations) is verified. For instance: for (int i = 0; i < maxi; ++i) for (int j = 0; j < maxj; ++j) // But if i == 4 < maxi AND j == 3 < maxj, // then jump out of the two nested loops. I know that this is possible in Perl with the next LABEL or last LABEL calls and labeled blocks, is it possible to do it in C++ or I should use a while loop? Thank you. 回答1: You can

Creating N nested for-loops

倾然丶 夕夏残阳落幕 提交于 2019-12-17 19:14:09
问题 Is there a way to create for-loops of a form for(int i = 0; i < 9; ++i) { for(int j = 0; j < 9; ++i) { //... for(int k = 0; k < 9; ++k) { //N-th loop without knowing N at the compile time. Ideally I'm trying to figure out a way to loop through separate elements of a vector of digits to create each possible number if a certain amount of digits is replaced with different digits. 回答1: You may use recursion instead with a base condition - void doRecursion(int baseCondition){ if(baseCondition==0)

Passing argument from Parent function to nested function Python

做~自己de王妃 提交于 2019-12-17 07:51:17
问题 here is my code: def f(x): def g(n): if n < 10: x = x + 1 g(n + 1) g(0) When I evaluate f(0), there would be an error "x referenced before assignment". However, when I use "print x" instead of "x = x + 1" , it will work. It seems that in the scope of g, I can only use x as an "use occurrence" but not a "binding occurrence". I guess the problem is that f passes to g only the VALUE of x. Am I understanding it correctly or not? If not, can someone explain why the left side of "x = x + 1" is not

Single Line Nested For Loops

放肆的年华 提交于 2019-12-17 06:26:26
问题 Wrote this function in python that transposes a matrix: def transpose(m): height = len(m) width = len(m[0]) return [ [ m[i][j] for i in range(0, height) ] for j in range(0, width) ] In the process I realized I don't fully understand how single line nested for loops execute. Please help me understand by answering the following questions: What is the order in which this for loop executes? If I had a triple nested for loop, what order would it execute? What would be equal the equal unnested for