Jump to content

C programming help

Rheostat.
Go to solution Solved by Ominous,
#include <stdio.h>

int main(void)
{
  int numbers[5];
  int i;

  for(i = 0; i <= 4; i++)
  {
   printf("Enter number: ");
   scanf("%d", &numbers[i]);
  }

  for(i = 0; i <= 4; i++)
  {
   printf("%d\n", numbers[i]);
   if (numbers[i] == 0) break;  // add this line
  }

  return(0);
}

 

I have no idea how to do this: "If the user enters 1 2 3 4 5, you read all the values and print all 5. If the user enters 1 2 3 0, you stop asking for more values and only print the first four values of the array etc"

 

#include <stdio.h>

int main(void)
{
  int numbers[5];
  int i;

  for(i = 0; i <= 4; i++)
  {
   printf("Enter number: ");
   scanf("%d", &numbers[i]);
  }

  for(i = 0; i <= 4; i++)
  {
   printf("%d\n", numbers[i]);
  }

  return(0);
}

 

"Don't turn it on, take it apart!"

   - David L. Jones

Link to comment
Share on other sites

Link to post
Share on other sites

 

1 hour ago, Rheostat. said:

I have no idea how to do this: "If the user enters 1 2 3 4 5, you read all the values and print all 5. If the user enters 1 2 3 0, you stop asking for more values and only print the first four values of the array etc"

That's too ambiguous.  When do you stop asking for more values? I'm assuming when a 0 is entered? Or does it literally mean when (1, 2, 3, 0) is entered? 

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, codesidian said:

 

That's too ambiguous.  When do you stop asking for more values? I'm assuming when a 0 is entered? Or does it literally mean when (1, 2, 3, 0) is entered? 

If 1, 2, 3, 4, 5 is entered it prints 1, 2, 3, 4, 5 from the array

 

But if 0 is entered 1, 2, 3, 0, 5 it prints 1, 2, 3, 0 from the array.

"Don't turn it on, take it apart!"

   - David L. Jones

Link to comment
Share on other sites

Link to post
Share on other sites

#include <stdio.h>

int main(void)
{
  int numbers[5];
  int i;

  for(i = 0; i <= 4; i++)
  {
   printf("Enter number: ");
   scanf("%d", &numbers[i]);
  }

  for(i = 0; i <= 4; i++)
  {
   printf("%d\n", numbers[i]);
   if (numbers[i] == 0) break;  // add this line
  }

  return(0);
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  int *numbers = NULL;
  int input, count = 0;

  do {
      printf("Enter number: ");
      scanf ("%d", &input);
      count++;
      numbers = (int*) realloc (numbers, count * sizeof(int));
      numbers[count-1]=input;
  } while (input !=0);
  
  for (int i=0; i < count; i++) printf("%d\n", numbers[i]);
  free(numbers);
  
  return(0);
}

or

#include <stdio.h>

int main(void)
{
  int numbers[4] = {};
  int input, count = 0;
  
  size_t numSize = sizeof(numbers) / sizeof(int);

  for (count=0; count <= numSize; count++)
  {
      printf("Enter number: ");
      scanf ("%d", &input);
      numbers[count]=input;
      if(input == 0) break;
  }
  
  for (int i=0; i <= count && i <= numSize; i++) 
      printf("%d\n", numbers[i]);
  
  return(0);
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×