loop inside another loop - iphone

I have this code:
//Value of userCount is 25
auxCount = 0;
for (int y_axis=0; y_axis<=8; y_axis++) //ROWS
{
for (int x_axis=0; x_axis<=2; x_axis++) //COLUMNS
{
if (auxCount<userCount) {
NSLog(#"auxCount: %i\n",auxCount);
NSLog(#"userCount: %i\n\n",userCount);
UIButton *btn= [[UIButton alloc] initWithFrame:CGRectMake(16+100*x_axis,115.0*y_axis,88.0 ,88.0)];
UILabel *userLabel = [[UILabel alloc] initWithFrame:CGRectMake(16+100*x_axis,90+115.0*y_axis, 88.0, 15.0)];
userLabel.textAlignment = UITextAlignmentCenter;
userLabel.text = mensaje;
btn.backgroundColor=[UIColor groupTableViewBackgroundColor];
[scrollViewUsers addSubview:btn];
[scrollViewUsers addSubview:userLabel];
auxCount++;
}
}
}
With this I want a matrix with 3 columns and X rows, but only displays 3 rows and the third row only displays 1 button. And in Debug area appears:
auxCount: 0
userCount: 25
auxCount: 4
userCount: 25
auxCount: 8
userCount: 25
auxCount: 12
userCount: 25
auxCount: 16
userCount: 25
auxCount: 20
userCount: 25
auxCount: 24
userCount: 25
auxCount increments by 4 by 4. I think that is because the if instructions are executed only by the first for loop but I don't know why.
Please, I need your help.
ps: sorry for my english!!

just try this for loops as i have used in my app it worked for me.
to find out the number of rows depending on number of columns and total count
int r;
float rem = [dao libraryCount] % kCol;
if(rem == 0.0f)
r = floor([dao libraryCount]/kD);
else
r = ceil([dao libraryCount]/kD);
here r is for number if row [dao libraryCount is total number of items and kCol is fix number of column for you it is 3 and kD is same as kCol only deference is it is type of float ie 3.0 in your case
then use the for loop as below
for (int row = 0; row < r; ++row)
{
for (int col = 0; col < kCol; ++col)
{
//Your Code to display or any thing
}
}
for auxCount use this code
int index = (row * kCol) + col;
if(index < [dao libraryCount])
{
//Your Code to display or any thing
}
put this in side both for loop instead of using ++
just change the both for loops and replace if condition with appropriate variables
Enjoy Coding :)
And Good Luck
any help is needed just comment me i will love to help you

Related

what is the difference between these 2 for loop in dart?

i just started learning dart but there is something i cant figure out.
the first loop it prints me from 1 3 5 7 9
the second one it prints for me from 0 to 9.
why did it remove the even numbers from the loop below? i only added a variable in the first loop
void main () {
for(double a = 0; a <10 ; a++)
{
double b = a++;
print (a);
}
print("---");
for(double a = 0; a <10 ; a++)
{
print(a);
}
}
the a++ is a shortcut to a = a +1, which means that b in each step is getting the value of a+1 and the second loop would only print odd numbers since you are jumping 2 steps in each loop (a++ in the loop brackets and the b = a++)

Solving Twitter Puddle with Zipper

This is a follow-up to my previous question. Consider the following puzzle
I would like to generate a waterLevel array, so that the i-th item is the water level at the i-th point and then sum them up to solve the puzzle.
waterLevel[i] =
max(0, min(max of left neighbors, max of right neighbors) - height[i])
I would probably try to code it with Zipper
waterLevels = heights.toZipper.cobind {z =>
max(0, min(max(z.left), max(z.right)) - z.focus
}.toList
Does it make sense ?
My solution with java, it comes with tests with expected solution:
package com.company;
import java.util.*;
enum Orientation {DOWN, UP};
class Walls{
public static void main(String []args){
HashMap<String, Integer> tests = new HashMap<String,Integer>();
tests.put("2 5 1 2 3 4 7 7 6", 10);
tests.put("2 2 5 1 3 1 2 1 7 7 6", 17);
tests.put("2 7 2 7 4 7 1 7 3 7", 18);
tests.put("4 6 7 7 4 3 2 1 5 2", 10);
tests.put("5 2 5 1 2 3 4 7 7 6 2 7 1 2 3 4 5 5 4", 26);
Iterator it = tests.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
it.remove();
String[] strings = ((String)pairs.getKey()).split(" ");
int[] walls = new int[strings.length];
for (int i = 0; i < walls.length; i++){
walls[i] = Integer.parseInt(strings[i].trim());
}
System.out.println(pairs.getKey()+" result="+accumulatedWater(walls)+" expected= " +pairs.getValue());
}
}
static int accumulatedWater(int []wall){
int MAX = 0;
int start = 0;
for(int i=0;i < wall.length;i++){ //let's go to the first peak
if(wall[i] >= MAX){
MAX = wall[i];
start = i;
}else{
break;
}
}
int []accumulate_max = new int[MAX+1]; // sums up to certain height
int []accumulate_max_step = new int[MAX+1]; // steps up to certain height
Orientation going = Orientation.DOWN;
int prev = MAX;
int local_sum=0;
int total_sum=0;
int PREVPEAK = MAX;
for(int i=start+1; i< wall.length; i++){
if( i == wall.length -1 ||
wall[i] < prev && going == Orientation.UP ){
going = Orientation.DOWN;
if(wall[i-1] >= MAX){
total_sum += accumulate_max_step[MAX-1] * MAX - accumulate_max[MAX-1];
MAX = wall[i-1];
PREVPEAK = MAX;
accumulate_max = new int[MAX+1];
accumulate_max_step = new int[MAX+1];
local_sum = 0;
}else{
int indexNewPeak = (i == wall.length -1 && wall[i]> wall[i-1]) ? i : i-1;
int water = accumulate_max_step[wall[indexNewPeak]-1] * wall[indexNewPeak] - accumulate_max[wall[indexNewPeak]-1];
if(wall[indexNewPeak] > PREVPEAK){
local_sum = water;
PREVPEAK = wall[indexNewPeak];
}else{
local_sum += water;
}
}
}else if(wall[i]>prev){
going = Orientation.UP;
}
for(int j=wall[i];j <= MAX;j++){
accumulate_max[j] += wall[i];
accumulate_max_step[j] += 1;
}
prev = wall[i];
}
return total_sum + local_sum;
}
}

iphone: how to check uitableview row modulus

it's a hard to explain issue so i hope i can state it
i have a tableview and i want that..
row number 1 have a background and row number 2 have another background .
and row number 3 have a third background..
the fourth row should have the first background and the fifth should have the second ..
and so forth ..
i used this code
if(row+1%1==0){
bg = [UIImage imageNamed:#"row1.png"];
selectionBg = [UIImage imageNamed:#"row1.png"];
}
else if(row+1%2==0){
bg = [UIImage imageNamed:#"row2.png"];
selectionBg = [UIImage imageNamed:#"row2.png"];
}else if(row+1%3==0){
bg = [UIImage imageNamed:#"row3.png"];
selectionBg = [UIImage imageNamed:#"row3.png"];
}
i'm trying to use modulus but i get lost .. so is there a way for that?
thanks in advance
You should use mod 3 because you have three options:
if (row % 3 == 0) {
// Option A
} else if (row % 3 == 1) {
// Option B
} else {
// Option C
}
By the way, do you understand what a modulo operation does? This might be interesting to read (from Wikipedia):
In computing, the modulo operation finds the remainder of division of
one number by another.
Given two positive numbers, a (the dividend)
and n (the divisor), a modulo n (abbreviated as a mod n) can be
thought of as the remainder, on division of a by n. For instance, the
expression "5 mod 4" would evaluate to 1 because 5 divided by 4 leaves
a remainder of 1, while "9 mod 3" would evaluate to 0 because the
division of 9 by 3 leaves a remainder of 0; there is nothing to
subtract from 9 after multiplying 3 times 3.
This is what happens in the code:
row row % 3 option
0 0 A
1 1 B
2 2 C
3 0 A
4 1 B
5 2 C
6 0 A
… … …
Instead of using the modulos use a static counter
Like the following
static int rowBGSelector = 0;
switch (rowBGSelector) {
case 0:
NSLog(#"%d, first", i); //Chose BG 1
break;
case 1:
NSLog(#"%d, second", i); //Chose BG 2
break;
case 2:
NSLog(#"%d, third", i); //Chose BG 3
break;
default:
rowBGSelector = -1; //Reset the static
break;
}
rowBGSelector++; //Increment

simple number series

This is a simple number series question, I have numbers in series like
2,4,8,16,32,64,128,256 these numbers are formed by 2,2(square),2(cube) and so on.
Now if I add 2+4+8 = 14. 14 will get only by the addition 2,4 and 8.
so i have 14in my hand now, By some logic i need to get the values which are helped to get 14
Example:
2+4+8 = 14
14(some logic) = 2,4,8.
This is an easy one:
2+4+8=14 ... 14+2=16
2+4+8+16=30 ... 30+2=32
2+4+8+16+32=62 ... 62+2=64
So you just need to add 2 to your sum, then calculate ld (binary logarithm), and then subtract 1. This gives you the number of elements of your sequence you need to add up.
e.g. in PHP:
$target=14;
$count=log($target+2)/log(2)-1;
echo $count;
gives 3, so you have to add the first 3 elements of your sequence to get 14.
Check the following C# code:
x = 14; // In your case
indices = new List<int>();
for (var i = 31; i >= i; i--)
{
var pow = Math.Pow(2, i);
if x - pow >= 0)
{
indices.Add(pow);
x -= pow;
}
}
indices.Reverse();
assuming C:
unsigned int a = 14;
while( a>>=1)
{
printf("%d ", a+1);
}
if this is programming, something like this would suffice:
int myval = 14;
int maxval = 256;
string elements = "";
for (int i = 1; i <= maxval; i*=2)
{
if ((myval & i) != 0)
elements += "," + i.ToString();
}
Use congruency module 2-powers: 14 mod 2 = 0, 14 mod 4 = 2, 14 mod 8 = 6, 14 mod 16 = 14, 14 mod 32 = 14...
The differences of this sequence are the numbers you look for 2 - 0 = 2, 6 - 2 = 4, 14 - 6 = 8, 14 - 14 = 0, ...
It's called the p-adic representation and is formally a bit more difficult to explain, but I hope this gives you an idea for an algorithm.

Split a integer into its separate digits

Say I have an integer, 9802, is there a way I can split that value in the four individual digits : 9, 8, 0 & 2 ?
Keep doing modulo-10 and divide-by-10:
int n; // from somewhere
while (n) { digit = n % 10; n /= 10; }
This spits out the digits from least-significant to most-significant. You can clearly generalise this to any number base.
You probably want to use mod and divide to get these digits.
Something like:
Grab first digit:
Parse digit: 9802 mod 10 = 2
Remove digit: (int)(9802 / 10) = 980
Grab second digit:
Parse digit: 980 mod 10 = 0
Remove digit: (int)(980 / 10) = 98
Something like that.
if you need to display the digits in the same order you will need to do the module twice visa verse this is the code doing that:
#import <Foundation/Foundation.h>
int main (int argc, char * argv[])
{
#autoreleasepool {
int number1, number2=0 , right_digit , count=0;
NSLog (#"Enter your number.");
scanf ("%i", &number);
do {
right_digit = number1 % 10;
number1 /= 10;
For(int i=0 ;i<count; i++)
{
right_digit = right_digit*10;
}
Number2+= right_digit;
Count++;
}
while ( number != 0 );
do {
right_digit = number2 % 10;
number2 /= 10;
Nslog(#”digit = %i”, number2);
}
while ( number != 0 );
}
}
return 0;
}
i hope that it is useful :)