Thread: Help with arrays: how to input array size and characters

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    1

    Help with arrays: how to input array size and characters

    Hey guys i need help with a program
    The first function outputs the n elements stored in the array, and the second function sorted the array using the basic bubble sort.
    i'm unsure of how to use arrays properly so i don't know exactly how to print and input both size and numbers in an array
    i'm support to sort the array

    i think at least some parts of the code may be right but i'm sure what i'm doing right or wrong as i'm not sure about how to use arrays properly
    any help would be grateful thanks


    Code:
    #include <stdlib.h>#include <stdio.h>
    #include <string.h>
    
    
    void intSwap(int *x, int *y);
    int getIntArray(int a[], int nmax, int sentinel);
    void printArray(int a[], int n);
    
    
    
    
    int main()
    {
    int i, j, temp, pass, size, array;
    
    
    scanf("%d", &array);
    /*print array*/
    /*sorts the array*/
    /*sorts printed array*/
    int x[100];
       int hmny;
    
    
       array = getIntArray(x, 100, 0);
       printf("The array was: \n");
       printArray(x,array);
       printf("after reverse it is:\n");
       printArray(x,array);
    
    
    
    
    
    
    }
    /*void intSwap(int *x, int *y)
    /* It swaps the content of x and y */
    /*
    {
       int temp = *x;
       *x = *y;
       *y = temp;
    }
    
    
    
    
    void bubbleSort(int a[], int size)
    {
         int pass, j, size, temp, end;
     pass =1;
      while (pass <= size-1)
      {
        j=0;
        end = size -1 - pass;
        while (j <= end)
        {
              if(a[j] > a[j+1])
              {temp = a[j]; a[j] = a[j+1]; a[j+1] = temp}
              j = j+1;
      }
      pass = pass + 1;
    }
    */
    /*
    void PrintArray(int a[], int n){
       int i;
       for (i=0; i<n; ){
             printf("\t%d ", a[i++]);
             if (i%5==0)
               printf("\n");
       }
       printf("\n");
    }
    */
    
    
    /* It reads up to nmax integers and stores then in a; sentinel
    * terminates input. */
    int getArray(int a[], int nmax, int sentinel)
    
    
       return n;
    
    
    }
    
    
    /*{
       int n = 0;
       int temp;
    while(temp<sentinel)
         printf("Enter integer [%d to terminate] : ", sentinel);
         scanf("%d", &temp);
         if (temp==sentinel) break;
         if (n==nmax)
           printf("array is full\n");
         else
         a[n++] = temp;
       }while (1);
       */

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by awkwardduckling View Post
    i think at least some parts of the code may be right but i'm sure what i'm doing right or wrong as i'm not sure about how to use arrays properly
    any help would be grateful thanks
    I suggest working step by step (read A development process). Let's start with your array input function:

    1) The name of the function must always be the same. In your declaration you call the function "getIntArray" but in the definition you call it "getArray". Choose one name and stick to it.

    2) The body of the function you commented out doesn't look too bad. Your while-syntax is wrong: either use a do-while-loop or an infinite while-loop. You have to break out of the loop if n == nmax. And you should return the number of elements in the array after you've read all elements.

    Try to implement this points and come back with more questions.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array size base on user input
    By icoigo in forum C Programming
    Replies: 2
    Last Post: 10-27-2010, 12:28 PM
  2. creating an array of input size..
    By transgalactic2 in forum C Programming
    Replies: 9
    Last Post: 12-22-2008, 04:34 AM
  3. Maximum array size (char) to input a double
    By salvadoravi in forum C Programming
    Replies: 10
    Last Post: 01-22-2008, 01:20 PM
  4. User input for array size.
    By coolmoniker in forum C++ Programming
    Replies: 27
    Last Post: 08-24-2006, 11:34 PM
  5. Keyboard input: Array of characters
    By metric1969 in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2003, 04:43 PM

Tags for this Thread