nested-loops

Factorial using Addition

允我心安 提交于 2019-12-02 08:43:53
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); } Erroneous Output: 20 -------------- 80 -------------- 240 -------------- 480 -------------- 480 Here's the

AlertController is being popped every time in nested conditions swift ios

谁都会走 提交于 2019-12-02 07:43:40
I have defined an alertcontroller when username or password is not correct the alert should pop, and it is working fine. but when the username & password is matched despite matching it pops up everytime when log in. I think I have not defined nested condition in a right way? help me to sort the multiple nested condition. Code for Login import UIKit import CoreData import Foundation class ViewController: UIViewController { var usernameGlobal : String = "" @IBOutlet weak var emailText: UITextField! @IBOutlet weak var passText: UITextField! @IBOutlet weak var loginButton: UIButton! @IBAction func

Nested loops result

倖福魔咒の 提交于 2019-12-02 07:05:29
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. Grijesh Chauhan In your pseudo-code, Inner most loop, k <- j to i+j can be written as k <- 0 to i ( this is by removing j ). Hence your code can be simplified as follows

How can I get rid of multiple nested for loops?

妖精的绣舞 提交于 2019-12-02 06:45:47
问题 I have a Python (3.2) script that searches for points with a property that I want. But it has this ugly part: for x in range(0,p): for y in range(0,p): for z in range(0,p): for s in range(0,p): for t in range(0,p): for w in range(0,p): for u in range(0,p): if isagoodpoint(x,y,z,s,t,w,u,p): print(x,y,z,s,t,w,u) else: pass Is there something I can do so that it looks a bit better? 回答1: You can use itertools to simplify your code: from itertools import product def print_good_points(p, dimensions

How to find out which nested for loop is better?

回眸只為那壹抹淺笑 提交于 2019-12-02 03:31:52
Somebody asked me a question: Which one is the fastest among the below two scenario: Case 1: assume int count = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 5; j++) { count++; } } Case 2: assume int count = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { count++; } } In both the scenario's the final valuse of count will be 50. But I am not sure which one will be faster? I think CASE II is faster but not sure... It will be great if anyone can throw some light on it. which is faster and why? this is the only example i can think of where it matters which variable you

inner loop with same variable name as outer loop

穿精又带淫゛_ 提交于 2019-12-01 20:38:43
问题 assuming the following simple code: for(int i=0; i < 1000; i++) { cout << "Outer i: " << i << endl; for(int i=0; i < 12; i++) { cout << "Inner i:" << i << endl; } } Works very nice. The same variable name in both loops used and the output is fine. Do I understand it right that both variables are created on stack, and when the outer loop comes to the new inner loop, a new "namespace" (maybe the wrong name..) is created? But why is it overwritten? If I choose another name for the variable in

How do nested loops in Java work? [closed]

谁都会走 提交于 2019-12-01 14:49:57
I am new to Java programming and trying to learn the basics of coding. I want to know how this snippet of code works? for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 10; j++) { System.out.print((i * j) + " "); } System.out.println(); } I will be really thankful if the programming sherlocks present here can explain me the logic. It will do... 1*1 1*2 1*3 till it gets to 1*10, then on a new line 2*1 2*2 2*3 and it will go to all the way to . . 5*10 So it will print out 1 2 3 4 5 ... till 10, then do a new line. Output below. 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24

How do nested loops in Java work? [closed]

心已入冬 提交于 2019-12-01 13:31:21
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 years ago . I am new to Java programming and trying to learn the basics of coding. I want to know how this snippet of code works? for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 10; j++) { System.out.print((i * j) + " "); } System.out.println(); } I will be really thankful if the programming sherlocks

Nested loops for creating a spiral shape pattern in c

两盒软妹~` 提交于 2019-12-01 10:39:31
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. 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 the top left quadrant as (a, b) int a = x; int b = y; if (a >= size / 2) a = size - a - 1; if (b >= size /

too many statically nested blocks python

人盡茶涼 提交于 2019-12-01 05:24:00
问题 I'm trying to write more than 21 lists containing the same number of items to columns in a text file. import random a=[] b=[] .... q=[] for i in range(200): a.append(random.gauss(10,0.1)) b.append(random(20,0.5)) .... q.append(random.gauss(50,0.2) for aVal in a: for bVal in b: .... for qVal in q: print(aVal, "\t ", bVal, ", ", .... , qVal) .... SystemError: too many statically nested blocks How can I write each list to a column in a text file? e.g. 0.892550 0.872493 0.206032 2.528080 0.722350