0

i m working on this small C program to fill an array, insert and remove elements from an array and finally printing that array.

The program consists of a main() function, the addToArray() function, remFromArray() function and the printArray() function.

here's my main method: which prompts the user for a sequence of positive integers

• for each integer entered by the user, call the addToArray function to insert the integer into the array

• call the printArray function to print out the content of the array.

NOTE:do not prompt the user for the number of integers to be entered; you must accept input until the user enters a negative number

#include <stdio.h>

#define maxSize 100 //maxSize for array

int addToArray(int* arr, int size, int numToAdd);
int remFromArray(int* arr, int size, int numToGo);
void printArray(int* arr, int size);

int main (void){

    int arr[maxSize];
    int i, j;

    printf("Enter a positive integer to add to an array\n");
    while (i >= 0 ){

        scanf("%d",&i);

        if (i >= 0 ) {
            addToArray(arr, maxSize, i);
        printf("Enter another positive integer for array\n");

        }
        else{
            printArray(arr, maxSize);
        }
     }
  }

these are my other function that i created:

addToArray() =>

  /*find the correct index in the array where to insert the 
    new element, so array in ascending order
  - make room for the new element by moving other elements
  - store the new element at the correct index
  - return the new size (number of elements) of the array */

int addToArray(int* arr, int size, int numToAdd){
int i, n, m, pos;   

    for (int i = 0; i < maxSize; i++)
    {
        if (numToAdd < arr[i])
        {
            pos = i;
            break;
        }
        if (numToAdd > arr[n-1])
        { 
            pos = maxSize;
            break;
        }
    }
    if (pos != maxSize)
    {
        m = maxSize - pos + 1 ;
        for (int i = 0; i <= m; i++)
        {
            arr[maxSize - i + 2] = arr[maxSize - i + 1] ;
        }
    }
    arr[pos] = numToAdd;
}

void printArray(int* arr, int size){

    printf("Resultant array is\n");

    for (int c = 0; c <= maxSize; c++){
        printf("%d\n", arr[c]); 
    }
}

as you can see I've tried writing the main(), addToArray(), printArray(), but for some reason its not working right. my printArray() is not displaying the desired result. I'll be glad if you could look at this code and guide me through it. thanks a million

niyasc
  • 4,440
  • 1
  • 23
  • 50
arsalunic612
  • 129
  • 2
  • 10
  • 2
    What do you get ? what do you expect to get ? – Pierre Sep 22 '16 at 07:41
  • 3
    You have *undefined behavior* because you don't initialize the array before you use the elements in it. Local non-static variables, including arrays and all their elements, have an *indeterminate* value if not explicitly initialized. And the array is not the only variable you use uninitialized. You also have undefined behavior because you go out of bounds of the array. – Some programmer dude Sep 22 '16 at 07:41
  • You check the value of `i` in the `while()` condition *before* reading in the value from the user. This makes no sense. – unwind Sep 22 '16 at 08:16
  • 1
    You may find the answer here [**Turning my array into a function and making 2nd function**](http://stackoverflow.com/questions/39610610/turning-my-array-into-a-function-and-making-2nd-function-to-print-it/39615736#39615736) helpful. – David C. Rankin Sep 22 '16 at 09:02

1 Answers1

3

First, you don't set values to the variables you create. For exemple, all of those int i, n, m, pos; have indeterminate values, not 0 as you might expect. Don't forget to asign them a value before using to avoid bad surprises.

Second, be carefull about indexes of your array. For example, arr[maxSize - i + 2], when i == 0, then the index will be maxSize + 2, witch is invalid position and can lead to segmentation fault.

informaticienzero
  • 1,796
  • 1
  • 12
  • 22
  • 3
    A couple of points: The value of uninitialized local non-static variables may *seem* random, but to nitpick the value is *indeterminate*. Also, undefined behavior (which is what happens when going out of bounds) doesn't *have* to lead to segmentation fault or a crash. – Some programmer dude Sep 22 '16 at 07:55