Write a program that reads the dimension N of an integer array T (maximum dimension: 50 components), fills the array with values entered on the keyboard and displays the array.
Then copy all strictly positive components into a second TPOS array and all strictly negative values into a third TNEG array. Display TPOS and TNEG arrays.
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define SIZE 50
void main()
{
int N; // nb of elements
int T[SIZE]; // array of maximum size 50
int TPOS[SIZE], TNEG[SIZE];
int i, j, k;
do {
printf("Enter N: ");
scanf("%d", &N);
} while (N<0 || N>50);
for (i = 0; i < N; i++)
{
printf("Enter T[%d]: ", i);
scanf("%d", &T[i]);
}
printf("\n\nPrinting the elements\n");
for (i = 0; i < N; i++)
{
printf("T[%d]=%d\n", i, T[i]);
}
j = k = 0;
for (i = 0; i < N; i++)
{
if (T[i] > 0)
TPOS[j++] = T[i];
else
if (T[i] < 0)
TNEG[k++] = T[i];
}
printf("\n\nPrinting the positif elements\n");
for (i = 0; i < j; i++)
{
printf("TPOS[%d]=%d\n", i, TPOS[i]);
}
printf("\n\nPrinting the negatif elements\n");
for (i = 0; i < k; i++)
{
printf("TNEG[%d]=%d\n", i, TNEG[i]);
}
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Topological sort using BFS traversal of a graph using an adjacency list