Skip to content

Commit e72cb87

Browse files
authored
Add files via upload
1 parent 35f5b16 commit e72cb87

File tree

1 file changed

+244
-0
lines changed

1 file changed

+244
-0
lines changed

Diff for: C/VariadicFunctions.c

+244
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
/*
2+
Variadic functions are functions which take a variable number of arguments. In C programming, a variadic function will contribute to the flexibility of the program that you are developing.
3+
4+
The declaration of a variadic function starts with the declaration of at least one named variable, and uses an ellipsis as the last parameter, e.g.
5+
6+
int printf(const char* format, ...);
7+
In this problem, you will implement three variadic functions named sum(),min() and max() to calculate sums, minima, maxima of a variable number of arguments. The first argument passed to the variadic function is the count of the number of arguments, which is followed by the arguments themselves.
8+
9+
Input Format
10+
11+
The first line of the input consists of an integer number_of_test_cases.
12+
Each test case tests the logic of your code by sending a test implementation of 3, 5 and 10 elements respectively.
13+
You can test your code against sample/custom input.
14+
The error log prints the parameters which are passed to the test implementation. It also prints the sum, minimum element and maximum element corresponding to your code.
15+
Constraints
16+
17+
1<=number_of_test_cases<=50
18+
1<=element<=1000000
19+
.
20+
21+
Output Format
22+
23+
"Correct Answer" is printed corresponding to each correct execution of a test implementation."Wrong Answer" is printed otherwise.
24+
25+
Sample Input 0
26+
27+
1
28+
Sample Output 0
29+
30+
Correct Answer
31+
Correct Answer
32+
Correct Answer
33+
*/
34+
35+
#include <stdarg.h>
36+
#include <stdio.h>
37+
#include <stdlib.h>
38+
#include <time.h>
39+
40+
#define MIN_ELEMENT 1
41+
#define MAX_ELEMENT 1000000
42+
int sum (int count,...) {
43+
44+
int sum;
45+
va_list all_elem;
46+
va_start(all_elem,count);
47+
48+
for(int i=0;i<count;i++)
49+
{
50+
int elem = va_arg(all_elem,int);
51+
sum+=elem;
52+
}
53+
va_end(all_elem);
54+
return sum;
55+
}
56+
57+
int min(int count,...) {
58+
59+
int min=MAX_ELEMENT;
60+
va_list all_elem;
61+
va_start(all_elem,count);
62+
63+
for(int i=0;i<count;i++)
64+
{
65+
int elem = va_arg(all_elem,int);
66+
if(elem<min)
67+
min=elem;
68+
}
69+
va_end(all_elem);
70+
return min;
71+
}
72+
73+
int max(int count,...) {
74+
75+
int max=MIN_ELEMENT;
76+
va_list all_elem;
77+
va_start(all_elem,count);
78+
79+
for(int i=0;i<count;i++)
80+
{
81+
int elem = va_arg(all_elem,int);
82+
if(elem>max)
83+
max=elem;
84+
}
85+
va_end(all_elem);
86+
return max;
87+
}
88+
89+
int test_implementations_by_sending_three_elements() {
90+
srand(time(NULL));
91+
92+
int elements[3];
93+
94+
elements[0] = rand() % (MAX_ELEMENT - MIN_ELEMENT + 1) + MIN_ELEMENT;
95+
elements[1] = rand() % (MAX_ELEMENT - MIN_ELEMENT + 1) + MIN_ELEMENT;
96+
elements[2] = rand() % (MAX_ELEMENT - MIN_ELEMENT + 1) + MIN_ELEMENT;
97+
98+
fprintf(stderr, "Sending following three elements:\n");
99+
for (int i = 0; i < 3; i++) {
100+
fprintf(stderr, "%d\n", elements[i]);
101+
}
102+
103+
int elements_sum = sum(3, elements[0], elements[1], elements[2]);
104+
int minimum_element = min(3, elements[0], elements[1], elements[2]);
105+
int maximum_element = max(3, elements[0], elements[1], elements[2]);
106+
107+
fprintf(stderr, "Your output is:\n");
108+
fprintf(stderr, "Elements sum is %d\n", elements_sum);
109+
fprintf(stderr, "Minimum element is %d\n", minimum_element);
110+
fprintf(stderr, "Maximum element is %d\n\n", maximum_element);
111+
112+
int expected_elements_sum = 0;
113+
for (int i = 0; i < 3; i++) {
114+
if (elements[i] < minimum_element) {
115+
return 0;
116+
}
117+
118+
if (elements[i] > maximum_element) {
119+
return 0;
120+
}
121+
122+
expected_elements_sum += elements[i];
123+
}
124+
125+
return elements_sum == expected_elements_sum;
126+
}
127+
128+
int test_implementations_by_sending_five_elements() {
129+
srand(time(NULL));
130+
131+
int elements[5];
132+
133+
elements[0] = rand() % (MAX_ELEMENT - MIN_ELEMENT + 1) + MIN_ELEMENT;
134+
elements[1] = rand() % (MAX_ELEMENT - MIN_ELEMENT + 1) + MIN_ELEMENT;
135+
elements[2] = rand() % (MAX_ELEMENT - MIN_ELEMENT + 1) + MIN_ELEMENT;
136+
elements[3] = rand() % (MAX_ELEMENT - MIN_ELEMENT + 1) + MIN_ELEMENT;
137+
elements[4] = rand() % (MAX_ELEMENT - MIN_ELEMENT + 1) + MIN_ELEMENT;
138+
139+
fprintf(stderr, "Sending following five elements:\n");
140+
for (int i = 0; i < 5; i++) {
141+
fprintf(stderr, "%d\n", elements[i]);
142+
}
143+
144+
int elements_sum = sum(5, elements[0], elements[1], elements[2], elements[3], elements[4]);
145+
int minimum_element = min(5, elements[0], elements[1], elements[2], elements[3], elements[4]);
146+
int maximum_element = max(5, elements[0], elements[1], elements[2], elements[3], elements[4]);
147+
148+
fprintf(stderr, "Your output is:\n");
149+
fprintf(stderr, "Elements sum is %d\n", elements_sum);
150+
fprintf(stderr, "Minimum element is %d\n", minimum_element);
151+
fprintf(stderr, "Maximum element is %d\n\n", maximum_element);
152+
153+
int expected_elements_sum = 0;
154+
for (int i = 0; i < 5; i++) {
155+
if (elements[i] < minimum_element) {
156+
return 0;
157+
}
158+
159+
if (elements[i] > maximum_element) {
160+
return 0;
161+
}
162+
163+
expected_elements_sum += elements[i];
164+
}
165+
166+
return elements_sum == expected_elements_sum;
167+
}
168+
169+
int test_implementations_by_sending_ten_elements() {
170+
srand(time(NULL));
171+
172+
int elements[10];
173+
174+
elements[0] = rand() % (MAX_ELEMENT - MIN_ELEMENT + 1) + MIN_ELEMENT;
175+
elements[1] = rand() % (MAX_ELEMENT - MIN_ELEMENT + 1) + MIN_ELEMENT;
176+
elements[2] = rand() % (MAX_ELEMENT - MIN_ELEMENT + 1) + MIN_ELEMENT;
177+
elements[3] = rand() % (MAX_ELEMENT - MIN_ELEMENT + 1) + MIN_ELEMENT;
178+
elements[4] = rand() % (MAX_ELEMENT - MIN_ELEMENT + 1) + MIN_ELEMENT;
179+
elements[5] = rand() % (MAX_ELEMENT - MIN_ELEMENT + 1) + MIN_ELEMENT;
180+
elements[6] = rand() % (MAX_ELEMENT - MIN_ELEMENT + 1) + MIN_ELEMENT;
181+
elements[7] = rand() % (MAX_ELEMENT - MIN_ELEMENT + 1) + MIN_ELEMENT;
182+
elements[8] = rand() % (MAX_ELEMENT - MIN_ELEMENT + 1) + MIN_ELEMENT;
183+
elements[9] = rand() % (MAX_ELEMENT - MIN_ELEMENT + 1) + MIN_ELEMENT;
184+
185+
fprintf(stderr, "Sending following ten elements:\n");
186+
for (int i = 0; i < 10; i++) {
187+
fprintf(stderr, "%d\n", elements[i]);
188+
}
189+
190+
int elements_sum = sum(10, elements[0], elements[1], elements[2], elements[3], elements[4],
191+
elements[5], elements[6], elements[7], elements[8], elements[9]);
192+
int minimum_element = min(10, elements[0], elements[1], elements[2], elements[3], elements[4],
193+
elements[5], elements[6], elements[7], elements[8], elements[9]);
194+
int maximum_element = max(10, elements[0], elements[1], elements[2], elements[3], elements[4],
195+
elements[5], elements[6], elements[7], elements[8], elements[9]);
196+
197+
fprintf(stderr, "Your output is:\n");
198+
fprintf(stderr, "Elements sum is %d\n", elements_sum);
199+
fprintf(stderr, "Minimum element is %d\n", minimum_element);
200+
fprintf(stderr, "Maximum element is %d\n\n", maximum_element);
201+
202+
int expected_elements_sum = 0;
203+
for (int i = 0; i < 10; i++) {
204+
if (elements[i] < minimum_element) {
205+
return 0;
206+
}
207+
208+
if (elements[i] > maximum_element) {
209+
return 0;
210+
}
211+
212+
expected_elements_sum += elements[i];
213+
}
214+
215+
return elements_sum == expected_elements_sum;
216+
}
217+
218+
int main ()
219+
{
220+
int number_of_test_cases;
221+
scanf("%d", &number_of_test_cases);
222+
223+
while (number_of_test_cases--) {
224+
if (test_implementations_by_sending_three_elements()) {
225+
printf("Correct Answer\n");
226+
} else {
227+
printf("Wrong Answer\n");
228+
}
229+
230+
if (test_implementations_by_sending_five_elements()) {
231+
printf("Correct Answer\n");
232+
} else {
233+
printf("Wrong Answer\n");
234+
}
235+
236+
if (test_implementations_by_sending_ten_elements()) {
237+
printf("Correct Answer\n");
238+
} else {
239+
printf("Wrong Answer\n");
240+
}
241+
}
242+
243+
return 0;
244+
}

0 commit comments

Comments
 (0)