I learned Objective-C before C and now that I am going back to C, I don't understand why the printf()
inside the loops does not work? Could someone advise me?
The program is the first challenge in the book "Programming Challenges" By Skiena and Revilla if anyone is wondering.
#include <stdio.h>
#include <stdbool.h>
static int inputInt;
static int secondInt;
int returnCycleNumber(int givenNumber);
int returnCycleNumber(int givenNumber) {
bool initial = true;
int counter = 1;
do
{
if (givenNumber % 2 != 0)
{
givenNumber = givenNumber * 3 + 1;
counter = counter + 1;
printf("\n%i", givenNumber);
}
else
{
givenNumber = givenNumber / 2;
counter = counter + 1;
printf("\n%i", givenNumber);
}
if (givenNumber == 1) {
initial = false;
}
} while (initial == true && givenNumber > 1);
return counter;
}
int main(int argc, const char * argv[])
{
scanf("%i %i", &inputInt, &secondInt);
fflush(stdout);
int arrayCount[secondInt];
for (int counter = 0; counter == (secondInt - inputInt); counter++ ) {
arrayCount[counter] = returnCycleNumber(inputInt + counter);
}
printf("\n%i", arrayCount[1]);
return 0;
}