- 19th Jan 2023
- 04:17 am
- Admin
C Programming Assignment Solution for Library Usage
#include
#include
int main()
{
int listOfNumber[100],index=0,number,found;
while(scanf("%d",&number))
{
found=1;
for(int i=0;i {
if(listOfNumber[i]==number)
found=0;
}
if(found==1)
{
listOfNumber[index]=number;
index++;
}
}
printf("[ ");
for(int i=0;i {
printf("%d ",listOfNumber[i]);
}
printf("]\n");
return 0;
}
The provided C code is a program that takes a list of numbers as input from the user and removes any duplicate numbers from the list. It stores the unique numbers in an array called listOfNumber.
Here's a brief explanation of how the program works:
-
The program initializes an array
listOfNumberof size 100 to store the unique numbers. It also initializes variablesindex(to keep track of the current index in thelistOfNumberarray),number(to read the user's input number), andfound(to check if the number is already present in the array). -
The program uses a
whileloop withscanfto read numbers from the user continuously until the user stops entering numbers (by pressing Ctrl+Z on Windows or Ctrl+D on Unix-based systems). -
For each number entered by the user, the program sets
foundto 1 (indicating the number is not found in the array yet) and iterates over the existing elements in thelistOfNumberarray. -
If the number is already present in the
listOfNumberarray, the program setsfoundto 0 (indicating the number is a duplicate). -
If the number is not a duplicate (i.e.,
foundis still 1), the program stores the number in thelistOfNumberarray at the current index (index) and increments theindex. -
After the user stops entering numbers, the program prints the final list of unique numbers stored in the
listOfNumberarray. -
The program then terminates by returning 0.
Example Input and Output:
Input: 1 2 3 2 4 5 1 6 7 5 8 9 10 (Enter Ctrl+D or Ctrl+Z to stop entering numbers)
Output: [ 1 2 3 4 5 6 7 8 9 10 ]
The program removes the duplicates (2, 1, and 5) and prints the final list of unique numbers in ascending order.
Please let me know if you have any specific questions or need further clarification on any part of the code! Get C programming Assignment Help now