Hello, i am currently working on the Flapjacks problem, The program compiles correctly, only it keeps going into infinity when i input, it keeps asking for more inputs, and its supposed to stop when i input -1.
Here is my Code.

#include <stdio.h>
#define SIZE 30
 
int isinorder (int []);
void flip (int [], int, int); 
  
int main ()
{  
int j, size, S[20], i, largest_position, largest, start, counter = 0;
   
while (size > 0)
{  
 scanf ("%d", &size);
 for (j = 0; j < size; j++)
{
scanf ("%d", &S[j]);
 }
} 
while (!isinorder (S))
{
 counter = SIZE;
 largest = 0;  
 for (i = 0; i = counter; i++)
 {
  if (S[i] > largest)
  {
   largest_position = i;
   largest = S[i];
 }
}  
}
flip (S, start, largest_position);
flip (S, start, counter);
  
  
for (i = 1; i <= counter; i++)
{
 printf ("%d ", &i);
 }
printf ("0\n");
   
}
 
int isinorder (int S[])
{  
 int i;
 for (i = 1; i < SIZE; i++)
  {
   if (S[i - 1] > S[i])
       return 0;
   }
return 1;
}void flip (int S[], int start, int largest_position)
{
int i, temp;
start = 0;
for (i = start; i < (largest_position - start)/2; i++)
  {
 temp = S[i];
 S[i] = S [largest_position - i];
 S [largest_position - i] = temp;
  }
}
int j, size, S[20], i, largest_position, largest, start, counter = 0;
    
    while (size > 0)
    {  
        scanf ("%d", &size);
        for (j = 0; j < size; j++)
        {
            scanf ("%d", &S[j]);
        }
    }

Perhaps it might be a good idea to initalize size] ?

hmm, that was very useful thanks, now my reading part of the code looks like this:

printf ("Please enter the number of pancakes to be stacked\n");
scanf ("%d", &size);
if (size ==-1){
break;
} else {
for (j = 1; j < size; j++)
 {
 scanf ("%d", &S[j]);
 }
}

it looks better but the problem is now i get an error that says :
""flapjacks.c", line 16: "break" outside loop or switch"
can you tell me why that is?
Thanks for the help

'break' can only be used to break out of a loop; while(){} or for(){} etc..
You are trying to break out of an if which is impossible. You should try something like this:

if (size !=-1)
{
    for (j = 1; j < size; j++)
    {
        scanf ("%d", &S[j]);
    }
}

It's the same thing only written slightly different.

Edited 6 Years Ago by Nick Evan: n/a

This article has been dead for over six months. Start a new discussion instead.