-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset_having_n_and_nsquared.c
More file actions
49 lines (34 loc) · 1.03 KB
/
set_having_n_and_nsquared.c
File metadata and controls
49 lines (34 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// A special set in which for every n there exists a unique n^2
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main() {
int set_len;
printf("Enter number of elements in the set: ");
if (scanf("%d", &set_len) != 1) {
printf("\nERROR: Wrong input given\n");
return -1;
}
int *arr = (int *)malloc(sizeof(int) * set_len * 2); // For accomodating the square elements too
if (arr == NULL) {
printf("\nERROR: Failed allocating array\n");
return -1;
}
// Input the elements
for (int i = 0; i < set_len * 2; i += 2) {
printf("Enter element for position (%d) -> ", (i / 2));
if (scanf("%d", (arr + i)) != 1) {
printf("\nERROR: Wrong input given\n");
free(arr);
return -1;
}
*(arr + i + 1) = *(arr + i) * *(arr + i); // Add square of element at i th position to j = (i + 1) th position
}
// Display the set
printf("The resultant set: (");
for (int i = 0; i < set_len * 2; i ++) {
printf(" %d ", *(arr + i));
}
printf(")\n");
free(arr);
}