Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,150,792 members, 7,810,060 topics. Date: Friday, 26 April 2024 at 07:45 PM |
Nairaland Forum / Science/Technology / Programming / Loops In Generalized Form (1528 Views)
Loops In Generalized Form by mexzony: 1:30pm On Jun 08, 2016 |
ok maybe am going nuts and this is stupid but here is my question. but first take a look at the following 1) for(int i = 1; i <= 4; i++) //this gives 1,2,3,4 i want to generate numbers in this pattern for when N = 1 or 2 or 3 or 4. generating all possible subsets where N = number of elements in an array. in this case i used N = 4. 1, 2 ,3 ,4 (1,2) (1,3) (1,4) (2,3) (2,4) (3,4) (1,2,3) (1,2,4) (1,3,4) (2,3,4) now when N is 5 or 6 or 7 or 8 how do i write a generalized loop that can sense the number of elements and come up with the subsets. Now i have been on this matter for days now and have seen solutions using recursion but i don't want to use the recursion solution looking at the code above you see that for each situation the single numbers need a "single" for loop while the pair of numbers need "two" for loops. my real question is "Is there a way to merge loops 1 and 2 above in a generalized manner or so so that if am writing another program and i say generate just single numbers in the array of say N elements, the program knows to use a single loop and if i say from this set of numbers in the array give me all possible combination of two numbers, the program knows it needs two loops that are nested and so on? any ideas programming is sweet hallelujah |
Re: Loops In Generalized Form by mexzony: 9:52pm On Jun 08, 2016 |
was finally able to figure out and solve the problem. cheers. |
Re: Loops In Generalized Form by romme2u: 1:10am On Jun 09, 2016 |
mexzony: oya share the testimony how u solve it. i hope u don't want me to preach on the benefits of giving testimonies |
Re: Loops In Generalized Form by mexzony: 7:55am On Jun 09, 2016 |
romme2u: I actually ditched the approach above as it was a time waster and went back to my earlier apporaoch where I use binary representation of each subset. Basically for N = 4 we will have 2^4 subsets which equals 16 subsets. Summarising with tables here is the trick Say we have 4 elements and we call each 1 2 3 4 Then 1 2 3 4 0 0 0 0 0 1 0 0 0 1 2 0 0 1 0 3 0 0 1 1 This goes all the way from 0 to (2^4 - 1) and u see the binary representstion of each number. For every position there is a 1 called a bit there is a subset so for 1 we have the subset {4} for 2 we have {3} For 3 we have {3,4}. This goes on like that. I like to be a little bit adventurous that's why I was trying to come up with so many solutions from different angles a times I just stress my self but its all part of the fun . |
Re: Loops In Generalized Form by romme2u: 6:12am On Jun 13, 2016 |
mexzony: this is more mathematical than logical. can u deduce the concept into a mathematical formula (possibly with pseudo-code) as the explanation is flying above my head. thanks |
Re: Loops In Generalized Form by benji93: 9:58am On Jun 13, 2016 |
Can anyone generate a programming code such that any number you enter above 65 prints out alphabets that leave a blank space in the middle in the shape of a diaomond For example ABCDEFGFEDCBA ABCDEFOFEDCBA ABCDEOOOEDCBA ABCDOOOOODCBA ABCOOOOOOOCBA ABOOOOOOOOOBA AOOOOOOOOOOOA ABOOOOOOOOOBA ABCOOOOOOOCBA ABCDOOOOODCBA ABCDEOOOEDCBA ABCDEFOFEDCBA ABCDEFGFEDCBA Meanwhile this code should be applicable to a higher number of consecutive alphabets beginning for from A The O's are just there to fill the spaces inorder for the patern to be printed in order |
Re: Loops In Generalized Form by mexzony: 11:17am On Jun 13, 2016 |
romme2u:Hello you should Google power set.you will have a better understanding. I can send you the code later but I would want you to try it out on your own. Also hope you know bitwise operators and how they work. |
Re: Loops In Generalized Form by benji93: 12:00pm On Jun 13, 2016 |
mexzony: There you are bro. You only need to enter the limit of the combination you want, as indicated, say 4,5,6,7,8........ If you need a logical explaination, incase u do not understand inform me, but try ot study it first. Meanwhile let me give an assignment bro. Write a code that will tell u if a number is prime or not.by uisng loops not functions. ALL THE BEST #include<stdio.h> int main() { int i,j,k; printf("ENTER LIMIT OF COMBINATION" scanf("%d",&k); for(int i = 1 ; i <= k-1; i++)// this gives (1,2) (1,3) (1,4) (2,3) (2,4) (3,4) { for(int j = i + 1; j <= k; j++) { printf("%d",i); printf("%d",j); printf("\n" } } } |
Re: Loops In Generalized Form by mexzony: 4:44pm On Jun 13, 2016 |
benji93:Thanks will study the code. As for the prime number. Have solved it before a long time ago.
|
Re: Loops In Generalized Form by benji93: 5:42pm On Jun 13, 2016 |
mexzony:I previously thought you were discusisng c programming languages, cos ur code is just not clear. |
Re: Loops In Generalized Form by mexzony: 7:40pm On Jun 13, 2016 |
benji93:Oh sorry I am writing in code/pseudocode Am using c# but the whole concept should be understandable. |
Re: Loops In Generalized Form by benji93: 11:02pm On Jun 13, 2016 |
mexzony:yep the logic correct. Have you tried the other code proble i posted.About the diamond shape.Using alphabets |
Re: Loops In Generalized Form by mexzony: 10:49am On Jun 14, 2016 |
benji93:Not yet a bit occupied.will do so for sure. |
(1) (Reply)
Mobile App Developers Check This Out / My New Year Resolutions As A Python Nigga / What is average salary package For Nigerian Software Development Firm
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 34 |