Thread: Rewriting Array code

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    7

    Rewriting Array code

    Code:
    #include <stdio.h>
    
    int main()
    {
    int arr[10];
    for(int i=0;i<10;i++)     //Inserting 10 arrays
    {
    printf("Insert Array %d\n",i+1);
    scanf("%d",&arr[i]); 
    }
    int n=10,a,min;    //Finded smallest array 
    min = arr[0];
    for(a=1; a<n; a++)
    { 
    if(arr[a]<min)
        { 
        min = arr[a];
        } 
    }
    printf("Smallest number in array = %d\n", min);
    return 0;
    }
    Sorry guys for stupid question but..sitting 2 days but I cannot write a simple code. Code needs to tell me to rewrite Array number if its(-1 or over 50) thanks for help..
    Last edited by dabraa; 11-15-2020 at 09:28 AM. Reason: found a mistake

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,416
    Something like
    Code:
    do {
      printf("Insert Array %d\n",i+1);
      scanf("%d",&arr[i]); 
      if ( arr[i] < 0 || arr[i] > 50 ) {
        printf("Out of range\n");
      } else {
        break;
      }
    } while ( 1 );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    7
    For some reasons it still doesnt want to work.. maybe you can copy-paste the whole code?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,416
    Maybe you can show what you tried.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2020
    Posts
    7
    Code:
     #include <stdio.h>
    
    int main()
    {
    int arr[10];
    for(int i=0;i<10;i++)  //Inserting 10 arrays
    {
    printf("Insert Array %d\n",i+1);
    scanf("%d",&arr[i]);
    }
    do ( arr[i] < 0 || arr[i] > 50 ) {
        printf("Out of range\n");
          scanf("%d",&arr[i]);
      } else {
        break;
      }
    } while ( 1 );
    
    int n=10,a,min;    //Found smallest array
    min = arr[0];
    for(a=1; a<n; a++)
    {
    if(arr[a]<min)
        {
        min = arr[a];
        }
    }
    printf("Smallest number in array = %d\n", min);
    return 0;
    }

  6. #6
    Registered User
    Join Date
    Nov 2020
    Posts
    7
    Code:
    #include <stdio.h>
    
    int main()
    {
        int x[10], min = 999999, i;
    
    
        for (i = 0; i < 10; i++)
        {
            int num;
    
    
            do {
                scanf("%d", &num);
    
    
                if (num < 0 || num > 49)
                    printf("Wrong number\n");
    
    
            } while (num < 0 || num > 49);
    
    
            x[i] = num;
            if (num < min)
                min = num;
        }
    
    
        printf("Minimum value: %d", min);
    
    
        return 0;
    If anyone needs this code, thanks for help Salem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rewriting a file from string to int
    By DOLZero in forum C Programming
    Replies: 9
    Last Post: 09-26-2013, 08:01 PM
  2. need help on rewriting
    By lucky4ever in forum C Programming
    Replies: 13
    Last Post: 01-15-2010, 09:50 AM
  3. rewriting my server
    By xixpsychoxix in forum Networking/Device Communication
    Replies: 5
    Last Post: 07-01-2008, 07:50 PM
  4. Rewriting an opened file.
    By Daghdha in forum C Programming
    Replies: 5
    Last Post: 06-13-2002, 01:55 AM

Tags for this Thread