Making an incremental for loop end by a certain number in objective-c - objective-c

I'm trying to find a solution to this coding problem:
Create a for loop that will begin with a value of 5 and end with a value of 25. In each iteration, add the incrementing value to mathTotal. (HINT: the last value used INSIDE the loop should be 25)
But the way I can think of doing it returns with a final number for mathTotal of 26. I'm not sure how to manipulate the code to stop at 25 without actually doing the math to figure out what number to make the condition for the program to stop running.
This is what I have:
int mathTotal;
for(int i = 5; mathTotal <=25; i++) {
mathTotal = mathTotal + i;
}
I know this is a simple problem, but I'm learning how to code and don't want to move on without fully understanding something.
Thank you!

There are two major issues:
mathTotal is not initialized. You have to set an initial value.
int mathTotal = 0;
The upper border (the second parameter of the for loop) is defined as mathTotal <= 25 – rather than i <= 25 – which will be reached when i is 8.
for (int i = 5; i <=25; i++) {
mathTotal = mathTotal + i;
}

The traditional for loop in Objective-C is inherited from standard C and takes the following form:
for (/* Instantiate local variables*/ ; /* Condition to keep looping. */ ; /* End of loop expressions */)
{
// Do something.
}
For example, to print the numbers from 1 to 10, you could use the for loop:
for (int i = 1; i <= 10; i++)
{
NSLog(#"%d", i); //do something
}
This is logically equivilant to the following traditional for loop:
for (int i = 0; i < [yourArray count]; i++)
{
NSLog([myArrayOfStrings objectAtIndex:i]);
}
Your Doubt
int mathTotal = 0;
for (i = 5 = 0; i <=25 ; i++)
{
mathTotal = mathTotal + i;
}

Related

getch is beeing skipped in a while loop

I have this function, while checks for correct input which need to be between 1-6 but when I call this function it just skips all the getch and all the putchar. What am I doing wrong?
int firstNum = 0;
int secondNum = 0;
int thirdNum = 0;
int fourthNum = 0;
int counter = 0;
int counterMiss = 0;
int counterInPlace = 0;
int condition = 1;
while(firstNum<0||firstNum>6||secondNum<0||firstNum>6||secondNum<0||secondNum>6||thirdNum<0||thirdNum>6||fourthNum<0||fourthNum>6) //loop that checks for input correct
{
if(counter>0)
{
printf("enter ONLY numbers beween 1-6\n"); //if there were any incorrect input it will trigger
}
firstNum=getch();
putchar(firstNum);
secondNum=getch();
putchar(secondNum);
thirdNum=getch();
putchar(thirdNum);
fourthNum=getch();
putchar(fourthNum);
firstNum-=48;
secondNum-=48;
thirdNum-=48;
fourthNum-=48;
counter++;
}
If you want a number of identical variables, use an array, not separate scalar variables. Separate variables like this look ugly and will not scale.
Also, you're initializing the variables to zero, and then testing if they are less than zero or greater than six. They aren't, so your loop doesn't run.

Selection sort implementation, I am stuck at calculating time complexity for number of swaps

static int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
swap(arr, i, j);
count++;
}
}
}
Is this the correct implementation for selection sort? I am not getting O(n-1) complexity for swaps with this implementation.
Is this the correct implementation for selection sort?
It depends, logically what you are doing is correct. It sort using "find the max/min value in the array". But, in Selection Sort, usually you didn't need more than one swap in one iteration. You just save the max/min value in the array, then at the end you swap it with the i-th element
I am not getting O(n-1) complexity for swaps
did you mean n-1 times of swap? yes, it happen because you swap every times find a larger value not only on the largest value. You can try to rewrite your code like this:
static int count=0;
static int maximum=0;
for(int i=0;i<arr.length-1;i++){
maximum = i;
for(int j=i+1;j<arr.length;j++){
if(arr[j] > arr[maximum]){
maximum = j;
}
}
swap(arr[maximum],arr[i]);
count++;
}
Also, if you want to exact n-1 times swap, your iteration for i should changed too.

Finding the number of factors using a while loop

so i have this method that finds the number of factors of a given number. It works fine and everything but i am using a for loop and my teacher is wanting me to change it into a while loop to make it more efficient, ive tried to change it but i keep getting endless loop here is the code i have using a for loop what might be a good to change it to a while loop without using a break and only having one return statement in the whole method
public static int numberOfFactors(int num){
int i;
int total=0;
for(i=1;i<=num;i++){
if(num%i==0)
total++;
}
return (total);}
I fail to see how:
i = 1;
while(i <= num) {
// do things
i++;
}
Is any more efficient than:
for( i=1; i<=num; i++) {
// do things
}
As far as I can tell? It's not! I'd love to know why your teacher thinks it is.
That said, here's what you can do to make it more efficient:
Calculate the square root of num and put it in sqrtnum as an integer, rounded down.
Change your loop to for(i=1; i<sqrtnum; i++) (note <, not <=)
If num%i==0, increment total by 2, instead of 1.
After the loop, check if sqrtnum*sqrtnum == num - if so, increment total by 1.
In this way, you only have to loop through a fraction of the numbers ;)
Not any more efficient but....
public static int numberOfFactors(int num) {
int total = 0;
int i = 1;
while(i <= num) {
if(num%i == 0)
total++;
i++;
}
return total;
}

Simple objective-c program doesn't work

Does anyone know what is wrong with this code?
int squareOf = 1500;
int squarer = 1;
for(int i = 0;i <= squareOf; i++){
for(;squarer <= i; squarer++){
if(i / squarer == 0){
NSLog(#"%i",i);
}
}
It doesn't give me any errors, just it doesn't output anything. Anyone know why? Sorry if it's painfully obvious, I'm quite new to programming.
This line:
if(i / squarer == 0)
It's never been called, put logs around the code and you'll understand:
for(int i = 0;i <= squareOf; i++){
for(;squarer <= i; squarer++){
NSLog(#"%d - %d", i, squarer)
if(i / squarer == 0){
NSLog(#"%i",i);
}
}
During the first iteration the inner loop doesn't start, because squarer is 1, and i is 0, so the for condition is never met.
During the second iteration the inner loop is executed once, because i is 1 and also squarer is 1. i/squarer is 1 so it doesn't print anything.
During the third loop squarer is already 2 (incremented in the previous loop), i is 2 so the loop gets executed once, and i/squarer is again 1.
You can easily imagine what does happen in all the other loops: i is always equal to squarer, i/squarer is always 1. It is totally useless to use two nested loops for this purpose, see this code:
int squareOf = 1500;
int squarer = 1;
for(int i=1; i<=squareOf; i++) {
if(i / squarer == 0)
NSLog(#"%i",i);
else
NSLog(#"i/squarer is not zero");
}
It is the equivalent, i/squarer will never be zero, I added a log statement for clarity. Now you understand why i/squarer is always 1? If your intention was to write a different code, please tell me what you are trying to achieve, and I can try to correct the code.

My program doesn't get into my second for loop

While doing some work for my lab in university
I am creating this function where there is a for loop inside another one.
It is not important to know what the method is used for. I just can't figure out why the program doesn't enter the second for loop. This is the code:
public void worseFit(int[] array){
int tempPosition = -1;
int tempWeight = 101 ;
for (int x = 0; x < (array.length - 1); x++){
if (allCrates.getSize() < 1){
Crate crate = new Crate();
crate.addWeight(array[0]);
allCrates.add(crate);
} else{
for( int i = 1; i < (allCrates.getSize() - 1); i++ ){
Crate element = allCrates.getElement(i);
int weight = element.getTotalWeight();
if (weight < tempWeight){
tempWeight = weight;
tempPosition = i;
Crate crate = new Crate();
if (weight + tempWeight <= 100){
crate.addWeight(weight + tempWeight);
allCrates.setElement(i, crate);
} else {
crate.addWeight(weight);
allCrates.setElement(allCrates.getSize(), crate);
} // if
} // if
} // for
} // if
} // for
} // worseFit
Once the program enters the else part of the code it goes straight
away back to the beginning of the first for loop.
Would anyone know how to solve this problem?
There seems to be some discrepancies with the expected values of allCrates.getSize().
If allCrates.getSize() returns 2, it will go to the second for loop, but not run it, as i < allCrates.getSize() - 1 will result in false
You might want to use <= instead of <
Initialize the variable i in your second loop to 0 instead of 1. Because if your getSize() returns 1 the it will not enter the if part and after entering the else part the for loop condition will evaluate to false and hence your for loop will not be executed.