Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Rheostat.

C programming help

#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);
}

 

Recommended Posts

Posted · Original PosterOP

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 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 post
Share on other sites
Posted · Original PosterOP
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 post
Share on other sites
Posted · Best Answer
#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 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);
}

 


My System - OS: Fedora Linux CPU: Ryzen 3600 @ 4.1 Ghz 1.33v Motherboard: Asus Strix X470-I Gaming RAM: 2x16GB Corsair Vengeance RGB Pro @ 3200 CL16 GPU: MSI Radeon 5700XT Mech OC

Wifes System - OS: Arch Linux CPU: Ryzen 3600 @ 4.1 Ghz 1.32v Motherboard: Asus Crosshair VI Hero X370 RAM: 2x8GB Corsair Vengeance RGB Pro @ 3200 CL16 GPU: AMD 5700XT Reference Card

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


×