-
Posts
1331 -
Joined
-
Last visited
Single Status Update
-
EDIT: Nevermind, I misunderstood it at first. I thought that when I add "6", it would add to array[5] (or the sixth element of the array). Problem solved.
This is for an assignment. I'm not asking for the solution, but I'm a bit confused:
Write a C program to create and manipulate a one dimensional array (list) of up to a hundred sorted (ascending order) numbers by using functions to insert, remove, or print elements as follows:
- Function insert: will search for the appropriate position of a given element and if the element is already in the list will display an error message. If not, the function will shift all the elements starting from the position of the element to the right of the array and then insert the element into that position.
- Function remove: will search for the element to be removed and if not found will display an error message. If the element exists, the function will remove it and shift all the elements that follow it to the left of the array.
- Function print: will print the elements that exist in the array at that point.
Your program should display a menu to the user that allows him/her to insert, remove, or print as many elements as they want until they want to stop. If an element is inserted into an already full list, an error message should be displayed. The array (list) at the beginning should be empty.
Example of a sample run:
Enter your choice: 1) insert 2) remove 3) print 4) exit
2
List is empty, No change
Enter your choice: 1) insert 2) remove 3) print 4) exit
1
Enter element to insert
6
Element 6 is inserted
Enter your choice: 1) insert 2) remove 3) print 4) exit
1
Enter element to insert
9
Element 9 is inserted
Enter your choice: 1) insert 2) remove 3) print 4) exit
1
Enter element to insert
7
Element 7 is inserted
Enter your choice: 1) insert 2) remove 3) print 4) exit
2
Enter element to remove
112
Element 112 does not exist, No change
Enter your choice: 1) insert 2) remove 3) print 4) exit
2
Enter element to remove
6
Element 6 is removed
Enter your choice: 1) insert 2) remove 3) print 4) exit
3
Elements in list are:
7
9
Enter your choice: 1) insert 2) remove 3) print 4) exit
1
Enter element to insert
7
Element 7 already exists, No change
Enter your choice: 1) insert 2) remove 3) print 4) exit
4
Goodbye
It seems to me that the example is wrong, shouldn't element 7 become 6? And what if the 100th element (99) is shifted to the right? They didn't say anything about the limits.