user_id
stringlengths 24
24
| time
stringlengths 24
24
| feature_type
stringclasses 5
values | feature_version
stringclasses 2
values | input_question
stringlengths 1
1.24k
⌀ | input_code
stringlengths 2
18.5k
⌀ | input_intention
stringlengths 1
2.48k
⌀ | input_task_description
stringlengths 18
3.32k
⌀ |
---|---|---|---|---|---|---|---|
63c647d75f004bf4898cf72a | 2023-01-27T00:52:14.255Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
/* Return a pointer to an array of two dynamically allocated arrays of ints.
The first array contains the elements of the input array s that are
at even indices. The second array contains the elements of the input
array s that are at odd indices.
Do not allocate any more memory than necessary. You are not permitted
to include math.h. You can do the math with modulo arithmetic and integer
division.
*/
int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
}
return arr_indexes;
}
/* Return a pointer to an array of ints with size elements.
- strs is an array of strings where each element is the string
representation of an integer.
- size is the size of the array
*/
int *build_array(char **strs, int size) {
int **ints = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
ints[i] = (int *) strtol(strs[i], NULL, 10);
}
return *ints;
}
int main(int argc, char **argv) {
/* Replace the comments in the next two lines with the appropriate
arguments. Do not add any additional lines of code to the main
function or make other changes.
*/
int *full_array = build_array(argv, sizeof(argv) / sizeof(char));
int **result = split_array(full_array, sizeof(argv) / sizeof(char));
printf("Original array:\n");
for (int i = 0; i < argc - 1; i++) {
printf("%d ", full_array[i]);
}
printf("\n");
printf("result[0]:\n");
for (int i = 0; i < argc / 2; i++) {
printf("%d ", result[0][i]);
}
printf("\n");
printf("result[1]:\n");
for (int i = 0; i < (argc - 1) / 2; i++) {
printf("%d ", result[1][i]);
}
printf("\n");
free(full_array);
free(result[0]);
free(result[1]);
free(result);
return 0;
}
| see doctoring | null |
63c647d75f004bf4898cf72a | 2023-01-27T00:53:48.571Z | Question from Code | v1 | does this function raise segmentation fault | int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
}
return arr_indexes;
} | null | null |
63c647d75f004bf4898cf72a | 2023-01-27T00:54:34.389Z | Question from Code | v1 | does this code raise segmentation fault | int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(*int));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
}
return arr_indexes;
} | null | null |
63c647d75f004bf4898cf72a | 2023-01-27T00:55:21.009Z | Question from Code | v1 | does this function raise segmentation fault | int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int*));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
}
return arr_indexes;
} | null | null |
63c647d75f004bf4898cf72a | 2023-01-27T00:56:47.211Z | Question from Code | v1 | does this code raise segmentation fault | int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int*));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
return arr_indexes;
} | null | null |
63c647d75f004bf4898cf72a | 2023-01-27T00:59:14.839Z | Question from Code | v1 | how do I fix segmentation fault | int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int*));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
}
arr_indexes[0] = &even_pt;
arr_indexes[1] = &odd_pt;
return arr_indexes;
} | null | null |
63c647d75f004bf4898cf72a | 2023-01-27T01:02:16.071Z | Help Fix Code | v1 | null | int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int*));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
return arr_indexes;
} | segmentation fault needs fix | null |
63c647d75f004bf4898cf72a | 2023-01-27T01:04:14.022Z | Help Fix Code | v1 | null | int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int*));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
return arr_indexes;
} | Return a pointer to an array of two dynamically allocated arrays of ints.
The first array contains the elements of the input array s that are
at even indices. The second array contains the elements of the input
array s that are at odd indices.
Do not allocate any more memory than necessary. You are not permitted
to include math.h. You can do the math with modulo arithmetic and integer
division. | null |
63c647d75f004bf4898cf72a | 2023-01-27T01:05:02.342Z | Help Fix Code | v1 | null | int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int*));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
return arr_indexes;
} | Return a pointer to an array of two dynamically allocated arrays of ints.
The first array contains the elements of the input array s that are
at even indices. The second array contains the elements of the input
array s that are at odd indices.
Do not allocate any more memory than necessary. You are not permitted
to include math.h. You can do the math with modulo arithmetic and integer
division. | null |
63c647d75f004bf4898cf72a | 2023-01-27T01:05:27.038Z | Help Fix Code | v1 | null | int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int*));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
return arr_indexes;
} | Return a pointer to an array of two dynamically allocated arrays of ints.
The first array contains the elements of the input array s that are
at even indices. The second array contains the elements of the input
array s that are at odd indices.
Do not allocate any more memory than necessary. You are not permitted
to include math.h. You can do the math with modulo arithmetic and integer
division. | null |
63c647d75f004bf4898cf72a | 2023-01-27T01:06:03.109Z | Question from Code | v1 | what's wrong with the code with behaviour described as:
Return a pointer to an array of two dynamically allocated arrays of ints.
The first array contains the elements of the input array s that are
at even indices. The second array contains the elements of the input
array s that are at odd indices.
Do not allocate any more memory than necessary. You are not permitted
to include math.h. You can do the math with modulo arithmetic and integer
division. | int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int*));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
return arr_indexes;
} | null | null |
63c647d75f004bf4898cf72a | 2023-01-27T01:06:33.657Z | Question from Code | v1 | how do I fix the code with behaviour described as: Return a pointer to an array of two dynamically allocated arrays of ints. The first array contains the elements of the input array s that are at even indices. The second array contains the elements of the input array s that are at odd indices. Do not allocate any more memory than necessary. You are not permitted to include math.h. You can do the math with modulo arithmetic and integer division. | int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int*));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
return arr_indexes;
} | null | null |
63c647d75f004bf4898cf72a | 2023-01-27T01:18:12.428Z | Question from Code | v1 | what's wrong with the code |
/* Return a pointer to an array of ints with size elements.
- strs is an array of strings where each element is the string
representation of an integer.
- size is the size of the array
*/
int *build_array(char **strs, int size) {
int *ints = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
ints[i] = (int) strtol(strs[i], NULL, 10);
}
return ints;
}
| null | null |
63c647d75f004bf4898cf72a | 2023-01-27T01:18:38.005Z | Question from Code | v1 | what's wrong with this function? |
/* Return a pointer to an array of ints with size elements.
- strs is an array of strings where each element is the string
representation of an integer.
- size is the size of the array
*/
int *build_array(char **strs, int size) {
int *ints = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
ints[i] = (int) strtol(strs[i], NULL, 10);
}
return ints;
}
| null | null |
63c647d75f004bf4898cf72a | 2023-01-27T01:19:11.858Z | Help Fix Code | v1 | null |
int *build_array(char **strs, int size) {
int *ints = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
ints[i] = (int) strtol(strs[i], NULL, 10);
}
return ints;
}
|
/* Return a pointer to an array of ints with size elements.
- strs is an array of strings where each element is the string
representation of an integer.
- size is the size of the array
*/
| null |
63c647d75f004bf4898cf72a | 2023-01-27T01:19:34.690Z | Help Fix Code | v1 | null |
int *build_array(char **strs, int size) {
int *ints = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
ints[i] = (int) strtol(strs[i], NULL, 10);
}
return ints;
}
|
Return a pointer to an array of ints with size elements.
- strs is an array of strings where each element is the string
representation of an integer.
- size is the size of the array
| null |
63c647d75f004bf4898cf72a | 2023-01-27T01:20:06.049Z | Explain Code | v1 | null |
int *build_array(char **strs, int size) {
int *ints = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
ints[i] = (int) strtol(strs[i], NULL, 10);
}
return ints;
}
| null | null |
63c647d75f004bf4898cf72a | 2023-01-27T01:22:04.315Z | Explain Code | v1 | null | int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int*));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
return arr_indexes;
} | null | null |
63c647d75f004bf4898cf72a | 2023-01-27T01:25:14.977Z | Question from Code | v1 | which line causes error | int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int*));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
return arr_indexes;
} | null | null |
63c647d75f004bf4898cf72a | 2023-01-27T01:26:06.497Z | Question from Code | v1 | why does arr_indexes[0] = even_pt; raise an error and how to fix it? | int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int*));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
return arr_indexes;
} | null | null |
63c647d75f004bf4898cf72a | 2023-01-27T01:28:29.519Z | Question from Code | v1 | which line causes error and how to fix? | #include <stdio.h>
#include <stdlib.h>
/* Return a pointer to an array of two dynamically allocated arrays of ints.
The first array contains the elements of the input array s that are
at even indices. The second array contains the elements of the input
array s that are at odd indices.
Do not allocate any more memory than necessary. You are not permitted
to include math.h. You can do the math with modulo arithmetic and integer
division.
*/
int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int*));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
}
// arr_indexes[0] = even_pt;
// arr_indexes[1] = odd_pt;
return arr_indexes;
}
/* Return a pointer to an array of ints with size elements.
- strs is an array of strings where each element is the string
representation of an integer.
- size is the size of the array
*/
int *build_array(char **strs, int size) {
int *ints = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
ints[i] = (int) strtol(strs[i], NULL, 10);
}
return ints;
}
int main(int argc, char **argv) {
/* Replace the comments in the next two lines with the appropriate
arguments. Do not add any additional lines of code to the main
function or make other changes.
*/
int *full_array = build_array(argv, sizeof(argv) / sizeof(char));
int **result = split_array(full_array, sizeof(argv) / sizeof(char));
printf("Original array:\n");
for (int i = 0; i < argc - 1; i++) {
printf("%d ", full_array[i]);
}
printf("\n");
printf("result[0]:\n");
for (int i = 0; i < argc / 2; i++) {
printf("%d ", result[0][i]);
}
printf("\n");
printf("result[1]:\n");
for (int i = 0; i < (argc - 1) / 2; i++) {
printf("%d ", result[1][i]);
}
printf("\n");
free(full_array);
free(result[0]);
free(result[1]);
free(result);
return 0;
}
| null | null |
63c647d75f004bf4898cf72a | 2023-01-27T01:46:25.966Z | Question from Code | v1 | why does this code result
Original array:
1 2 3
result[0]:
2 0
result[1]:
1
instead of
Original array:
1 2 3
result[0]:
1 3
result[1]:
2 | #include <stdio.h>
#include <stdlib.h>
/* Return a pointer to an array of two dynamically allocated arrays of ints.
The first array contains the elements of the input array s that are
at even indices. The second array contains the elements of the input
array s that are at odd indices.
Do not allocate any more memory than necessary. You are not permitted
to include math.h. You can do the math with modulo arithmetic and integer
division.
*/
int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int*));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
return arr_indexes;
}
/* Return a pointer to an array of ints with size elements.
- strs is an array of strings where each element is the string
representation of an integer.
- size is the size of the array
*/
int *build_array(char **strs, int size) {
int *ints = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
ints[i] = (int) strtol(strs[i], NULL, 10);
}
return ints;
}
int main(int argc, char **argv) {
/* Replace the comments in the next two lines with the appropriate
arguments. Do not add any additional lines of code to the main
function or make other changes.
*/
int *full_array = build_array(argv + 1, argc - 1);
int **result = split_array(full_array, argc - 1);
printf("Original array:\n");
for (int i = 0; i < argc - 1; i++) {
printf("%d ", full_array[i]);
}
printf("\n");
printf("result[0]:\n");
for (int i = 0; i < argc / 2; i++) {
printf("%d ", result[0][i]);
}
printf("\n");
printf("result[1]:\n");
for (int i = 0; i < (argc - 1) / 2; i++) {
printf("%d ", result[1][i]);
}
printf("\n");
free(full_array);
free(result[0]);
free(result[1]);
free(result);
return 0;
}
| null | null |
63c647d75f004bf4898cf72a | 2023-01-27T01:47:24.333Z | Question from Code | v1 | why is the output not as desired | #include <stdio.h>
#include <stdlib.h>
/* Return a pointer to an array of two dynamically allocated arrays of ints.
The first array contains the elements of the input array s that are
at even indices. The second array contains the elements of the input
array s that are at odd indices.
Do not allocate any more memory than necessary. You are not permitted
to include math.h. You can do the math with modulo arithmetic and integer
division.
*/
int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int*));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
return arr_indexes;
}
/* Return a pointer to an array of ints with size elements.
- strs is an array of strings where each element is the string
representation of an integer.
- size is the size of the array
*/
int *build_array(char **strs, int size) {
int *ints = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
ints[i] = (int) strtol(strs[i], NULL, 10);
}
return ints;
}
int main(int argc, char **argv) {
/* Replace the comments in the next two lines with the appropriate
arguments. Do not add any additional lines of code to the main
function or make other changes.
*/
int *full_array = build_array(argv + 1, argc - 1);
int **result = split_array(full_array, argc - 1);
printf("Original array:\n");
for (int i = 0; i < argc - 1; i++) {
printf("%d ", full_array[i]);
}
printf("\n");
printf("result[0]:\n");
for (int i = 0; i < argc / 2; i++) {
printf("%d ", result[0][i]);
}
printf("\n");
printf("result[1]:\n");
for (int i = 0; i < (argc - 1) / 2; i++) {
printf("%d ", result[1][i]);
}
printf("\n");
free(full_array);
free(result[0]);
free(result[1]);
free(result);
return 0;
}
| null | null |
63c647d75f004bf4898cf72a | 2023-01-27T01:48:37.104Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
/* Return a pointer to an array of two dynamically allocated arrays of ints.
The first array contains the elements of the input array s that are
at even indices. The second array contains the elements of the input
array s that are at odd indices.
Do not allocate any more memory than necessary. You are not permitted
to include math.h. You can do the math with modulo arithmetic and integer
division.
*/
int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int*));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
return arr_indexes;
}
/* Return a pointer to an array of ints with size elements.
- strs is an array of strings where each element is the string
representation of an integer.
- size is the size of the array
*/
int *build_array(char **strs, int size) {
int *ints = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
ints[i] = (int) strtol(strs[i], NULL, 10);
}
return ints;
}
int main(int argc, char **argv) {
/* Replace the comments in the next two lines with the appropriate
arguments. Do not add any additional lines of code to the main
function or make other changes.
*/
int *full_array = build_array(argv + 1, argc - 1);
int **result = split_array(full_array, argc - 1);
printf("Original array:\n");
for (int i = 0; i < argc - 1; i++) {
printf("%d ", full_array[i]);
}
printf("\n");
printf("result[0]:\n");
for (int i = 0; i < argc / 2; i++) {
printf("%d ", result[0][i]);
}
printf("\n");
printf("result[1]:\n");
for (int i = 0; i < (argc - 1) / 2; i++) {
printf("%d ", result[1][i]);
}
printf("\n");
free(full_array);
free(result[0]);
free(result[1]);
free(result);
return 0;
}
| - | null |
63c647d75f004bf4898cf72a | 2023-01-27T01:50:07.891Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
/* Return a pointer to an array of two dynamically allocated arrays of ints.
The first array contains the elements of the input array s that are
at even indices. The second array contains the elements of the input
array s that are at odd indices.
Do not allocate any more memory than necessary. You are not permitted
to include math.h. You can do the math with modulo arithmetic and integer
division.
*/
int **split_array(const int *s, int length) {
int **arr_indexes = malloc(2 * sizeof(int*));
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_count += 1;
} else {
odd_count += 1;
}
}
int *even_pt = malloc(even_count * sizeof(int));
int *odd_pt = malloc(odd_count * sizeof(int));
int even_idx = 0;
int odd_idx = 0;
for (int i = 0; i < length; i++) {
if (s[i] % 2 == 0) {
even_pt[even_idx] = s[i];
even_idx += 1;
} else {
odd_pt[odd_idx] = s[i];
odd_idx += 1;
}
}
arr_indexes[0] = even_pt;
arr_indexes[1] = odd_pt;
return arr_indexes;
}
/* Return a pointer to an array of ints with size elements.
- strs is an array of strings where each element is the string
representation of an integer.
- size is the size of the array
*/
int *build_array(char **strs, int size) {
int *ints = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
ints[i] = (int) strtol(strs[i], NULL, 10);
}
return ints;
}
int main(int argc, char **argv) {
/* Replace the comments in the next two lines with the appropriate
arguments. Do not add any additional lines of code to the main
function or make other changes.
*/
int *full_array = build_array(argv + 1, argc - 1);
int **result = split_array(full_array, argc - 1);
printf("Original array:\n");
for (int i = 0; i < argc - 1; i++) {
printf("%d ", full_array[i]);
}
printf("\n");
printf("result[0]:\n");
for (int i = 0; i < argc / 2; i++) {
printf("%d ", result[0][i]);
}
printf("\n");
printf("result[1]:\n");
for (int i = 0; i < (argc - 1) / 2; i++) {
printf("%d ", result[1][i]);
}
printf("\n");
free(full_array);
free(result[0]);
free(result[1]);
free(result);
return 0;
}
| return array of two pointers to one with odd other with even | null |
63c647d75f004bf4898cf72a | 2023-01-27T18:55:04.054Z | Question from Code | v1 | with input ./count_large 1000 rwx------
with command line input
total 329
-rwx------ 1 reid staff 1734 Jun 22 14:52 prog
-rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt
-rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz
-rw-r--r-- 1 reid staff 88 Feb 15 2013 xyz.c
why does the function return 1 instead of 2 | #include <stdio.h>
#include <stdlib.h>
// TODO: Implement a helper named check_permissions that matches the prototype below.
// int check_permissions(char *, char *);
int check_permissions(char *a, char *b) {
for (int i = 0; i < 9; i++) {
if (b[i] == '-') {
;
} else if (b[i] != a[i]) {
return 1;
}
}
return 0;
}
int main(int argc, char** argv) {
if (!(argc == 2 || argc == 3)) {
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
// TODO: Process command line arguments.
// char input[50];
int count = 0;
int val = 0;
char permissions[10];
int file_size;
scanf("%*s, %*d");
while (val != -1) {
val = scanf("%s %*d %*s %*s %d %*s %*d %*s %*s", permissions, &file_size);
if (val == -1) {
break;
}
// printf("%s ", permissions);
// printf("%d\n", file_size);
int size = strtol(argv[1], NULL, 10);
if (file_size >= size) {
if (argc == 3) {
if (check_permissions(permissions, argv[2]) == 0) {
count += 1;
}
} else {
count += 1;
}
}
}
printf("%d \n", count ); | null | null |
63c647d75f004bf4898cf72a | 2023-01-27T19:03:14.749Z | Question from Code | v1 | what's wrong with this code | #include <stdio.h>
#include <stdlib.h>
// TODO: Implement a helper named check_permissions that matches the prototype below.
// int check_permissions(char *, char *);
int check_permissions(char *a, char *b) {
for (int i = 0; i < 9; i++) {
if (b[i] == '-') {
;
} else if (b[i] != a[i]) {
return 1;
}
}
return 0;
}
int main(int argc, char** argv) {
if (!(argc == 2 || argc == 3)) {
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
// TODO: Process command line arguments.
// char input[50];
int count = 0;
int val = 0;
char permissions[10];
int file_size;
scanf("%*s, %*d");
while (val != -1) {
val = scanf("%s %*d %*s %*s %d %*s %*d %*s %*s", permissions, &file_size);
if (val == -1) {
break;
}
// printf("%s ", permissions);
// printf("%d\n", file_size);
int size = strtol(argv[1], NULL, 10);
if (file_size >= size) {
if (argc == 3) {
if (check_permissions(permissions, argv[2]) == 0) {
count += 1;
}
} else {
count += 1;
}
}
}
printf("%d \n", count );
| null | null |
63c647d75f004bf4898cf72a | 2023-01-27T19:03:51.456Z | Question from Code | v1 | which part of the code is wrong | #include <stdio.h>
#include <stdlib.h>
// TODO: Implement a helper named check_permissions that matches the prototype below.
// int check_permissions(char *, char *);
int check_permissions(char *a, char *b) {
for (int i = 0; i < 9; i++) {
if (b[i] == '-') {
;
} else if (b[i] != a[i]) {
return 1;
}
}
return 0;
}
int main(int argc, char** argv) {
if (!(argc == 2 || argc == 3)) {
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
// TODO: Process command line arguments.
// char input[50];
int count = 0;
int val = 0;
char permissions[10];
int file_size;
scanf("%*s, %*d");
while (val != -1) {
val = scanf("%s %*d %*s %*s %d %*s %*d %*s %*s", permissions, &file_size);
if (val == -1) {
break;
}
// printf("%s ", permissions);
// printf("%d\n", file_size);
int size = strtol(argv[1], NULL, 10);
if (file_size >= size) {
if (argc == 3) {
if (check_permissions(permissions, argv[2]) == 0) {
count += 1;
}
} else {
count += 1;
}
}
}
printf("%d \n", count );
| null | null |
63c647d75f004bf4898cf72a | 2023-01-27T19:05:34.075Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
// TODO: Implement a helper named check_permissions that matches the prototype below.
// int check_permissions(char *, char *);
int check_permissions(char *a, char *b) {
for (int i = 0; i < 9; i++) {
if (b[i] == '-') {
;
} else if (b[i] != a[i]) {
return 1;
}
}
return 0;
}
int main(int argc, char** argv) {
if (!(argc == 2 || argc == 3)) {
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
// TODO: Process command line arguments.
// char input[50];
int count = 0;
int val = 0;
char permissions[10];
int file_size;
scanf("%*s, %*d");
while (val != -1) {
val = scanf("%s %*d %*s %*s %d %*s %*d %*s %*s", permissions, &file_size);
if (val == -1) {
break;
}
// printf("%s ", permissions);
// printf("%d\n", file_size);
int size = strtol(argv[1], NULL, 10);
if (file_size >= size) {
if (argc == 3) {
if (check_permissions(permissions, argv[2]) == 0) {
count += 1;
}
} else {
count += 1;
}
}
}
printf("%d \n", count );
| print out permissions match and file size is larger or equal | null |
63c647d75f004bf4898cf72a | 2023-01-27T19:07:02.904Z | Explain Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
// TODO: Implement a helper named check_permissions that matches the prototype below.
// int check_permissions(char *, char *);
int check_permissions(char *a, char *b) {
for (int i = 0; i < 9; i++) {
if (b[i] == '-') {
;
} else if (b[i] != a[i]) {
return 1;
}
}
return 0;
}
int main(int argc, char** argv) {
if (!(argc == 2 || argc == 3)) {
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
// TODO: Process command line arguments.
// char input[50];
int count = 0;
int val = 0;
char permissions[10];
int file_size;
scanf("%*s, %*d");
while (val != -1) {
val = scanf("%s %*d %*s %*s %d %*s %*d %*s %*s", permissions, &file_size);
if (val == -1) {
break;
}
// printf("%s ", permissions);
// printf("%d\n", file_size);
int size = strtol(argv[1], NULL, 10);
if (file_size >= size) {
if (argc == 3) {
if (check_permissions(permissions, argv[2]) == 0) {
count += 1;
}
} else {
count += 1;
}
}
}
printf("%d \n", count );
| null | null |
63c647d75f004bf4898cf72a | 2023-01-28T16:22:55.551Z | General Question | v1 | how can I ignore first line in scanf
| null | null | null |
63c647d75f004bf4898cf72a | 2023-01-28T16:24:40.985Z | General Question | v1 | ignore the first character using scanf | null | null | null |
63c647d75f004bf4898cf72a | 2023-01-28T17:05:51.398Z | General Question | v1 | convert int to char[9] | null | null | null |
63c647d75f004bf4898cf72a | 2023-01-28T17:06:21.071Z | General Question | v1 | convert int to array | null | null | null |
63c647d75f004bf4898cf72a | 2023-01-28T17:10:18.028Z | General Question | v1 | index an int | null | null | null |
63c647d75f004bf4898cf72a | 2023-02-08T19:19:36.850Z | Question from Code | v1 | why does this raise segmentation fault | int create_user(const char *name, User **user_ptr_add) {
User *curr = user_ptr_add[0];
while (curr->next != NULL) {
if (strcmp(name, curr->name) == 0) {
return 1;
}
curr = curr->next;
} | null | null |
63c647d75f004bf4898cf72a | 2023-02-08T19:20:09.900Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
User *curr = user_ptr_add[0];
while (curr->next != NULL) {
if (strcmp(name, curr->name) == 0) {
return 1;
}
curr = curr->next;
} | fix the segmentation fault | null |
63c647d75f004bf4898cf72a | 2023-02-08T19:21:30.388Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
User *curr = *user_ptr_add;
while (curr->next != NULL) {
if (strcmp(name, curr->name) == 0) {
return 1;
}
curr = curr->next;
} | fix segmentation fault | null |
63c647d75f004bf4898cf72a | 2023-02-08T19:22:55.249Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
User *curr = *user_ptr_add;
while (curr != NULL) {
if (strcmp(name, curr->name) == 0) {
return 1;
}
curr = curr->next;
} | fix segmentation fault | null |
63c647d75f004bf4898cf72a | 2023-02-08T19:32:23.616Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
User *curr = *user_ptr_add;
while (curr != NULL) {
if (strcmp(name, curr->name) == 0) {
return 1;
}
curr = curr->next;
} | fix this code | null |
63c647d75f004bf4898cf72a | 2023-02-08T19:36:42.121Z | Question from Code | v1 | what are the problems of this function | int create_user(const char *name, User **user_ptr_add) {
User *curr = *user_ptr_add;
User *prev = NULL;
while (curr != NULL) {
if (strcmp(name, curr->name) == 0) {
return 1;
}
prev = curr;
curr = curr->next;
}
// This stage is reached if curr is NULL
if (strlen(name) > MAX_NAME - 1) {
return 2;
}
User *usr = malloc(sizeof(User));
strncpy(usr->name, name, MAX_NAME); // not a literal so we must copy
usr->next = NULL;
prev->next = usr;
return 0;
} | null | null |
63c647d75f004bf4898cf72a | 2023-03-08T19:19:53.305Z | Explain Code | v1 | null | /* Convert an array of args to a single space-separated string in buffer.
Returns buffer. Note that memory for args and buffer should be allocated
by the caller.
*/
char *args_to_string(char **args, char *buffer, int size) {
buffer[0] = '\0';
int i = 0;
while (args[i] != NULL) {
strncat(buffer, args[i], size - strlen(buffer));
strncat(buffer, " ", size - strlen(buffer));
i++;
}
return buffer;
}
| null | null |
63c647d75f004bf4898cf72a | 2023-03-08T19:20:31.315Z | Question from Code | v1 | what's a buffer? | /* Convert an array of args to a single space-separated string in buffer.
Returns buffer. Note that memory for args and buffer should be allocated
by the caller.
*/
char *args_to_string(char **args, char *buffer, int size) {
buffer[0] = '\0';
int i = 0;
while (args[i] != NULL) {
strncat(buffer, args[i], size - strlen(buffer));
strncat(buffer, " ", size - strlen(buffer));
i++;
}
return buffer;
}
| null | null |
63c647d75f004bf4898cf72a | 2023-03-10T22:43:54.277Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXLINE 256
#define MAX_PASSWORD 10
#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"
int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
/* The user will type in a user name on one line followed by a password
on the next.
DO NOT add any prompts. The only output of this program will be one
of the messages defined above.
Please read the comments in validate carefully
*/
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
// TODO
// Remove newline character from end of user_id
if (strlen(user_id) > MAX_PASSWORD-1) {
printf("%s", NO_USER);
return 0;
}
if (strlen(password) > MAX_PASSWORD-1) {
printf("%s", INVALID);
return 0;
}
int pipefd[2];
pid_t pid;
pipe(pipefd);
pid = fork();
if (pid == 0) {
close(pipefd[0]);
if (dup2(pipefd[1], STDOUT_FILENO) == -1) {
perror("dup2");
exit(1);
}
close(pipefd[1]);
execl("./validate", "validate", user_id, password, NULL);
perror("execl");
exit(1);
} else {
close(pipefd[1]);
int status;
pid_t wpid;
char buffer[MAXLINE];
ssize_t n;
while ((n = read(pipefd[0], buffer, MAXLINE)) > 0) {
write(STDOUT_FILENO, buffer, n);
}
close(pipefd[0]);
wpid = waitpid(pid, &status, 0);
if (wpid == -1) {
perror("waitpid");
exit(1);
}
wpid = waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
int exit_status = WEXITSTATUS(status);
if (exit_status == 0) {
printf("%s", SUCCESS);
} else if (exit_status == 2) {
printf("%s", INVALID);
} else {
printf("%s", NO_USER);
}
}
}
return 0;
} | Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.
Your program should use the exit status of the validate program to determine which of the three following messages to print:
"Password verified" if the user id and password match.
"Invalid password" if the user id exists, but the password does not match.
"No such user" if the user id is not recognized
The exact messages are given in the starter code as defined constants.
The only case that should be handled directly in checkpasswd is the case where either the userid or password are too long. This is to prevent sending the wrong number of bytes to validate.
Note that in the given password file pass.txt, the "killerwhales:swim" has a user id that is too large, and "monkeys:eatcoconuts" has a password that is too long. The examples are expected to fail, but the other cases should work correctly.
You will find the following system calls useful: fork, exec, pipe, dup2, write, wait (along with WIFEXITED, WEXITSTATUS). You may not use popen or pclose in your solution.
Important: execl arguments
Week 7 Video 6 "Running Different Programs" demonstrates a version of execl that takes only two arguments. The signature for execl is:
int execl(const char *path, const char *arg0, ... /*, (char *)NULL */);
In the video, the execl call only passed two arguments (execl("./hello", NULL)), but that shortcut doesn't work on teach.cs. Instead, you need to pass the middle argument (respresenting argv[0]) explicitly: execl("./hello", "hello", NULL).
Let's consider two more examples. If you want to call the executable ./giant with the arguments fee fi fo, you would do it like this: execl("./giant", "giant", "fee", "fi", "fo", NULL); If you want to call ./giant with no arguments you would call it like this: execl("./giant", "giant", NULL);
| null |
63c647db5f004bf4898cf748 | 2023-01-17T21:10:46.418Z | Explain Code | v1 | null | include <stdio.h>
int main() {
char name[30];
printf("Hello CSC209 students!\n");
fgets(name, 30, stdin);
printf("Hi %s\n", name);
return 0;
} | null | null |
63c647db5f004bf4898cf748 | 2023-01-17T21:12:19.765Z | General Question | v1 | what is standard input? | null | null | null |
63c647db5f004bf4898cf748 | 2023-01-18T15:49:29.893Z | General Question | v1 | what does scanf do in c? | null | null | null |
63c647db5f004bf4898cf748 | 2023-01-23T20:07:57.797Z | Explain Code | v1 | null | scanf("%[^\n]", line);
| null | null |
63c647db5f004bf4898cf748 | 2023-03-08T21:40:14.925Z | Explain Code | v1 | null | if (pid == 0) {
//child process; read from pipe and write to stdin of validate
close(pipefd[1]);
if (dup2(pipefd[0], STDIN_FILENO) == -1) {
perror("Dup2 failed");
exit(1);
}
close(pipefd[0]);
execl("./validate", "validate", NULL);
} | null | null |
63c647db5f004bf4898cf748 | 2023-03-08T21:41:10.077Z | Question from Code | v1 | when execl calls validate, where is the standard input read from? | if (pid == 0) {
//child process; read from pipe and write to stdin of validate
close(pipefd[1]);
if (dup2(pipefd[0], STDIN_FILENO) == -1) {
perror("Dup2 failed");
exit(1);
}
close(pipefd[0]);
execl("./validate", "validate", NULL);
} | null | null |
63c647db5f004bf4898cf748 | 2023-03-08T21:52:00.105Z | General Question | v1 | what does execl return?
| null | null | null |
63c647dc5f004bf4898cf752 | 2023-01-23T20:08:13.088Z | Help Fix Code | v1 | null | int main() {
char **last_names;
last_names = malloc(sizeof(char*));
last_names[0] = malloc(sizeof(char) * 20);
last_names[1] = malloc(sizeof(char) * 20);
last_names[2] = malloc(sizeof(char) * 20);
last_names[3] = malloc(sizeof(char) * 20);
return 0;
} | Assign a dynamically allocated char * array of length 4 to last_names.
Then, allocate a character array of length 20 for each element of the array pointed to by last_names.
| null |
63c647dc5f004bf4898cf752 | 2023-01-23T20:10:21.189Z | Help Fix Code | v1 | null |
}int main(int argc, char **argv) {
float *rainfall;
float rain_today;
// rainfall has been dynamically allocated space for a floating point number.
// Both rainfall and rain_today have been initialized in hidden code.
// Assign the amount in rain_today to the space rainfall points to.
return 0;
} | rainfall has been dynamically allocated space for a floating point number.
Both rainfall and rain_today have been initialized in hidden code.
Assign the amount in rain_today to the space rainfall points to.
| null |
63c647dc5f004bf4898cf752 | 2023-01-23T20:11:29.364Z | Help Fix Code | v1 | null | int main(int argc, char **argv) {
float *rainfall;
float rain_today;
// rainfall has been dynamically allocated space for a floating point number.
// Both rainfall and rain_today have been initialized in hidden code.
// Assign the amount in rain_today to the space rainfall points to.
// Submitted code below
rain_today = rainfall;
// Submitted code above
return 0;
} | rainfall has been dynamically allocated space for a floating point number.
Both rainfall and rain_today have been initialized in hidden code.
Assign the amount in rain_today to the space rainfall points to. | null |
63c647dc5f004bf4898cf752 | 2023-01-23T20:19:08.657Z | Help Fix Code | v1 | null | int get_sum(const char *input_line) {
}
| Returns the sum of two integers in the string input_line.
input_line is a string containing two integers separated by one
or more spaces.
Assume that the string will not be more than 20 characters
long and that it contains two integers separated only by whitespace.
There may also be spaces before the first integer
and/or after the last, but there won't be any other characters
in the string.
For example, get_sum(" 123 5 ") should return the integer 128,
and get_sum("-14 30") should return the integer 16.
Hint: use strtol().
| null |
63c647dc5f004bf4898cf752 | 2023-01-25T01:13:20.476Z | General Question | v1 | is the sizeof(char *) and sizeof(&char) the same? | null | null | null |
63c647dc5f004bf4898cf752 | 2023-01-25T17:29:19.026Z | Help Fix Code | v1 | null | int main(){
char greetings[15] = "salutations";
char *farewell[] = "goodbye";
}
| Define a stack-allocated string called greetings that has space for 15 characters (including the null terminator) and set its initial value to "salutations". Then define a string literal with the name farewell that has the value "goodbye". | null |
63c647dc5f004bf4898cf752 | 2023-01-25T17:30:24.660Z | Help Fix Code | v1 | null | int main(){
char greetings[15] = "salutations";
char *farewell = "goodbye";
}
| Define a stack-allocated string called greetings that has space for 15 characters (including the null terminator) and set its initial value to "salutations". Then define a string literal with the name farewell that has the value "goodbye". | null |
63c647dc5f004bf4898cf752 | 2023-01-25T17:36:45.462Z | Help Fix Code | v1 | null | int main(){
char greetings[15] = "salutations";
char *farewell = "goodbye";
}
| Define a stack-allocated string called greetings that has space for 15 characters (including the null terminator) and set its initial value to "salutations". Then define a string literal with the name farewell that has the value "goodbye".
| null |
63c647dc5f004bf4898cf752 | 2023-01-25T17:40:14.193Z | Help Fix Code | v1 | null | #include <studio.h>
int main() {
char greetings[15] = "salutations";
char *farewell = "goodbye";
}
| Define a stack-allocated string called greetings that has space for 15 characters (including the null terminator) and set its initial value to "salutations". Then define a string literal with the name farewell that has the value "goodbye". | null |
63c647dc5f004bf4898cf752 | 2023-01-25T17:50:49.710Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *sweet = malloc(20 * sizeof(char));
strcpy(*sweet, "tiramisu");
return 0;
} | The string sweet has been declared on the heap and has space for 20 characters.
Set the value of sweet to "tiramisu" without changing its address. | null |
63c647dc5f004bf4898cf752 | 2023-01-25T18:10:10.539Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct contact {
char name[30];
int phone_number;
char address[30];
};
void update_contact(struct contact *friend,char *name, int number, char* address){
strcpy((*friend).name, *name);
(*friend).phone_number = number;
strcpy((*friend).address, *address);
}
int main(int argc, char **argv) {
struct contact friend;
// set initial values for friend
strcpy(friend.name, "Jane Doe");
friend.phone_number = 377177377;
strcpy(friend.address, "3771 University Avenue");
char *name;
int number;
char *address;
// in hidden code set values for these 3 variables
// call update_contact to change the values in friend
update_contact(&friend, name, number, address);
return 0;
} | Given the following program, write the function update_contact which takes a pointer to the struct contact, a new name, a new number, and a new address to change the struct’s members. Look at the function call for the order of arguments and appropriate types. | null |
63c647dc5f004bf4898cf752 | 2023-01-25T22:50:55.418Z | Help Fix Code | v1 | null | struct Node{
int value;
struct Node *next;
}; | Create a node struct with the following struct members:
- value, an integer
- next, a pointer to another node
Typedef your structure as Node. | null |
63c647dc5f004bf4898cf752 | 2023-01-25T22:52:15.784Z | Help Fix Code | v1 | null | typedef struct Node{
int value;
struct Node *next;
}; | Create a node struct with the following struct members:
- value, an integer
- next, a pointer to another node
Typedef your structure as Node.
| null |
63c647dc5f004bf4898cf752 | 2023-01-27T00:50:44.124Z | Help Fix Code | v1 | null | int **split_array(const int *s, int length) {
int *arrays;
int half_length = ceil(length/2);
int *array1 = malloc(sizeof(int) * half_length);
int *array2 = malloc(sizeof(int) * half_length);
for (int i = 0; i < length; i = i + 2){
array1[i] = *s[i];
}
for (int i = 1; i < length; i = i + 2){
array2[i] = *s[i];
}
arrays[0] = *array1;
arrays[1] = *array2;
return arrays;
}
| Return a pointer to an array of two dynamically allocated arrays of ints.
The first array contains the elements of the input array s that are
at even indices. The second array contains the elements of the input
array s that are at odd indices.
Do not allocate any more memory than necessary. You are not permitted
to include math.h. You can do the math with modulo arithmetic and integer
division. | null |
63c647dc5f004bf4898cf752 | 2023-01-27T01:00:49.551Z | Question from Code | v1 | Why is 'arrays' uninitialized? | int **split_array(const int *s, int length) {
int **arrays;
int half_length;
if (length%2 == 0){
half_length = length/2 + 1;
}
else {
half_length = length/2;
}
int *array1 = malloc(sizeof(int) * half_length);
int *array2 = malloc(sizeof(int) * half_length);
for (int i = 0; i < length; i = i + 2){
array1[i] = s[i];
}
for (int i = 1; i < length; i = i + 2){
array2[i] = s[i];
}
arrays[0] = array1;
arrays[1] = array2;
return arrays;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-01-27T01:05:22.264Z | Help Fix Code | v1 | null | int *build_array(char **strs, int size) {
int *ptr;
for (int i = 0; i < size; i++){
ptr[i] = (int) strtol(strs[i], NULL, 10);
}
return ptr;
}
| Return a pointer to an array of ints with size elements.
- strs is an array of strings where each element is the string
representation of an integer.
- size is the size of the array | null |
63c647dc5f004bf4898cf752 | 2023-01-27T01:16:22.562Z | Help Fix Code | v1 | null | int *build_array(char **strs, int size) {
int *ptr = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++){
ptr[i] = (int) strtol(strs[i], NULL, 10);
}
return ptr;
}
| Return a pointer to an array of ints with size elements.
- strs is an array of strings where each element is the string
representation of an integer.
- size is the size of the array | null |
63c647dc5f004bf4898cf752 | 2023-01-27T01:18:42.644Z | Help Fix Code | v1 | null | int *build_array(char **strs, int size) {
int *ptr = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++){
ptr[i] = strtol(strs[i], NULL, 10);
}
return ptr;
}
| Return a pointer to an array of ints with size elements.
- strs is an array of strings where each element is the string
representation of an integer.
- size is the size of the array
| null |
63c647dc5f004bf4898cf752 | 2023-01-27T01:20:29.904Z | Help Fix Code | v1 | null | int *build_array(char **strs, int size) {
int *ptr = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++){
ptr[i] = strtol(strs[i], NULL, 10);
}
return ptr;
}
| Return a pointer to an array of ints with size elements.
- strs is an array of strings where each element is the string
representation of an integer.
- size is the size of the array | null |
63c647dc5f004bf4898cf752 | 2023-01-27T01:27:10.295Z | Help Fix Code | v1 | null | int **split_array(const int *s, int length) {
int half_length;
if (length%2 == 0){
half_length = length/2 + 1;
}
else {
half_length = length/2;
}
int *array1 = malloc(sizeof(int) * half_length);
int *array2 = malloc(sizeof(int) * half_length);
for (int i = 0; i < length; i = i + 2){
array1[i] = s[i];
}
for (int i = 1; i < length; i = i + 2){
array2[i] = s[i];
}
int **arrays = malloc(sizeof(int *) * 2);
arrays[0] = array1;
arrays[1] = array2;
return arrays;
}
| Return a pointer to an array of two dynamically allocated arrays of ints.
The first array contains the elements of the input array s that are
at even indices. The second array contains the elements of the input
array s that are at odd indices.
Do not allocate any more memory than necessary. You are not permitted
to include math.h. You can do the math with modulo arithmetic and integer
division. | null |
63c647dc5f004bf4898cf752 | 2023-01-27T02:54:47.321Z | Help Fix Code | v1 | null | int **split_array(const int *s, int length) {
int half_length;
int **arrays = malloc(sizeof(int *) * 2);
if (length%2 == 0){
length1 = length/2;
length2 = length/2;
}
else {
length1 = length/2;
length2 = length/2 + 1;
}
int *array1 = malloc(sizeof(int) * length1);
int *array2 = malloc(sizeof(int) * length2);
for (int i = 0; i < length; i = i + 2){
array1[i] = s[i];
}
for (int i = 1; i < length; i = i + 2){
array2[i] = s[i];
}
arrays[0] = array1;
arrays[1] = array2;
return arrays;
} | Return a pointer to an array of two dynamically allocated arrays of ints.
The first array contains the elements of the input array s that are
at even indices. The second array contains the elements of the input
array s that are at odd indices.
Do not allocate any more memory than necessary. You are not permitted
to include math.h. You can do the math with modulo arithmetic and integer
division. | null |
63c647dc5f004bf4898cf752 | 2023-01-27T03:02:40.718Z | Help Fix Code | v1 | null | int *build_array(char **strs, int size) {
int *ptr = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++){
ptr[i] = strtol(strs[i + 1], NULL, 10);
}
return ptr;
} | Return a pointer to an array of ints with size elements.
- strs is an array of strings where each element is the string
representation of an integer.
- size is the size of the array | null |
63c647dc5f004bf4898cf752 | 2023-01-27T03:34:33.833Z | Help Fix Code | v1 | null | int *fib(int **fib_sequence, int n){
int *fib = malloc(sizeof(int) * n);
*fib_sequence = fib;
int num1 = 0;
int num2 = 1;
for (int i = 0; i < n; i++){
fib[i] = num1 + num2;
num2 = fib[i];
num1 = num2;
}
return fib;
}
| Define a function void fib(...) below. This function takes parameter n
* and generates the first n values in the Fibonacci sequence. Recall that this
* sequence is defined as:
* 0, 1, 1, 2, 3, 5, ... , fib[n] = fib[n-2] + fib[n-1], ...
* The values should be stored in a dynamically-allocated array composed of
* exactly the correct number of integers. The values should be returned
* through a pointer parameter passed in as the first argument. | null |
63c647dc5f004bf4898cf752 | 2023-01-27T18:24:04.004Z | Help Fix Code | v1 | null | int *fib(int **fib_sequence, int n){
int *fib = malloc(sizeof(int) * n);
*fib_sequence = fib;
if (n == 0){
fib[0] = 0;
}
else if (n == 1){
fib[0] = 0;
fib[1] = 1;
}
else {
for (int i = 2; i < n; i++){
fib[i] = fib[i-2] + fib[i-1];
}
}
return fib;
}
| Define a function void fib(...) below. This function takes parameter n
* and generates the first n values in the Fibonacci sequence. Recall that this
* sequence is defined as:
* 0, 1, 1, 2, 3, 5, ... , fib[n] = fib[n-2] + fib[n-1], ...
* The values should be stored in a dynamically-allocated array composed of
* exactly the correct number of integers. The values should be returned
* through a pointer parameter passed in as the first argument.
*
* See the main function for an example call to fib.
* Pay attention to the expected type of fib's parameters.
| null |
63c647dc5f004bf4898cf752 | 2023-01-27T18:42:31.692Z | Help Fix Code | v1 | null | int check_permissions(char *file_permission, char *required_permission){
for (int i; i < 10; i++){
if ((file_permission[i] != required_permission[i]) && (required_permission[i] != '-')){
return 1;
}
}
return 0;
}
| You are required to write (and use) a helper function named check_permissions that takes two 9-element character arrays as arguments and returns an integer. (The prototype for this function is in the starter code.) The first array will represent the permission field of a file and the second will represent the permissions that are required. The function will return 0 if the file has all the required permissions and 1 otherwise. The arrays do not have to be identical for a 0 to be returned. For example, if the first array holds the characters rwxr-x--- and the second array holds the characters r-x------, the function should return 0. Often when we pass arrays as parameters to functions, we also pass a size so that the function can work on arrays of varying lengths. In this case we don't need to pass a size because both arrays will always have nine elements. | null |
63c647dc5f004bf4898cf752 | 2023-01-29T00:39:34.996Z | General Question | v1 | How do you get multiple command line arguments and save them as variables? | null | null | null |
63c647dc5f004bf4898cf752 | 2023-01-29T00:50:41.545Z | General Question | v1 | For example, given the output of ls -l:
total 329
-rwx------ 1 reid staff 1734 Jun 22 14:52 prog
-rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt
-rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz
-rw-r--r-- 1 reid staff 88 Feb 15 2013 xyz.c
how could I store each line in a separate string using scanf? | null | null | null |
63c647dc5f004bf4898cf752 | 2023-01-29T20:18:50.827Z | Help Fix Code | v1 | null | while (scanf("%10s %*d %*s %*s %d %*s %*d %*s %*s", permission, &file_size)!= EOF){
// printf("%d", file_size);
int count = 0;
if (file_size > size){
if (argc == 2){
count = count + 1;
}
// TODO: Call check_permissions as part of your solution to count the files to
// compute and print the correct value.
else if (argc == 3){
int a = check_permissions(permission, required);
printf("%d\n", a);
if (a==1){
count = count + 1;
}
}
}
}
printf("%s", permission);
printf("%d", count); | print permission and count | null |
63c647dc5f004bf4898cf752 | 2023-01-29T20:22:32.396Z | Help Fix Code | v1 | null | int main(int argc, char** argv) {
17 if (!(argc == 2 || argc == 3)) {
18 fprintf(stderr, "USAGE: count_large size [permissions]\n");
19 return 1;
20 }
21
22 int size;
23 char required[11];
24 int *count;
25
26 // TODO: Process command line arguments.
27 if (argc == 2){
28 size = *argv[1];
29 } else {
30 size = *argv[1];
31 required[11] = *argv[2];
32 }
33
34 // Process arguments passed from pipe
35 char permission[11];
36 int file_size;
37
38 while (scanf("%10s %*d %*s %*s %d %*s %*d %*s %*s", permission, &file_size)!= EOF){
39 // printf("%d", file_size);
40
41
42 if (file_size > size){
43 if (argc == 2){
44 *count = *count + 1;
45 }
46 // TODO: Call check_permissions as part of your solution to count the files to
47 // compute and print the correct value.
48 else if (argc == 3){
49 int a = check_permissions(permission, required);
50 printf("%d\n", a);
51 if (a==1){
52 *count = *count + 1;
53 }
54 }
55 }
56 }
57 printf("%s", permission);
58 printf("%d", *count);
59 return 0;
60 }
| warning: ‘count’ may be used uninitialized in this function | null |
63c647dc5f004bf4898cf752 | 2023-01-29T22:26:39.669Z | Help Fix Code | v1 | null | int populate_array(int sin, int *sin_array) {
// check if sin is 9 digits
int count = 0;
int num = sin;
while (num != 0){
count++;
num /= 10;
}
if (count != 9){
return 1;
}
// populate sin_array
for (int i = 8; i > 0; i--){
sin_array[i] = sin % 10;
sin = sin / 10;
}
return 0;
}
| It takes an integer and an integer array as its arguments, and returns an integer. This function's job is to populate the given integer array so that it contains the 9 digits of the given integer, in the same order as in the integer. Hint: use % 10 and / 10 to calculate the digits. The function must return 0 when it completes successfully, and 1 if the given integer is not 9 digits. | null |
63c647dc5f004bf4898cf752 | 2023-01-29T22:27:20.236Z | Help Fix Code | v1 | null | int populate_array(int sin, int *sin_array) {
// check if sin is 9 digits
int count = 0;
int num = sin;
while (num != 0){
count++;
num /= 10;
}
if (count != 9){
return 1;
}
// populate sin_array
for (int i = 8; i >= 0; i--){
sin_array[i] = sin % 10;
sin = sin / 10;
}
return 0;
}
| It takes an integer and an integer array as its arguments, and returns an integer. This function's job is to populate the given integer array so that it contains the 9 digits of the given integer, in the same order as in the integer. Hint: use % 10 and / 10 to calculate the digits. The function must return 0 when it completes successfully, and 1 if the given integer is not 9 digits. | null |
63c647dc5f004bf4898cf752 | 2023-01-29T22:44:57.046Z | Help Fix Code | v1 | null | int main(int argc, char **argv) {
// TODO: Verify that command line arguments are valid.
if (argc != 2){
return 2;
}
// TODO: Parse arguments and then call the two helpers in sin_helpers.c
// to verify the SIN given as a command line argument.
int *populated_array;
populate_array((int) argv[1], populated_array);
int b = check_sin(populated_array);
if (b == 0){
printf("%s\n", "Valid SIN");
return 0;
} else{
printf("%s\n", "Invalid SIN");
return 1;
}
}
| If the user calls the program with too few or too many arguments, the program should not print anything, but should return from main with return code 2. main should return 0 when the program runs successfully and the SIN is valid and 1 when the program runs successfully but the SIN is invalid. | null |
63c647dc5f004bf4898cf752 | 2023-01-30T02:53:01.831Z | Help Fix Code | v1 | null | int main(int argc, char** argv) {
if (!(argc == 2 || argc == 3)) {
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
int size;
char required[11];
int count = 0;
printf("argc: %d, argv: %s\n", argc, argv[2]);
// TODO: Process command line arguments.
if (argc == 2){
size = *argv[1];
} else {
size = *argv[1];
required = argv[2];
}
} | Set required as the string in argv[2] | null |
63c647dc5f004bf4898cf752 | 2023-02-02T21:45:32.961Z | Help Fix Code | v1 | null | int main(int argc, char ** argv){
if (argc != 3){
printf("Invalid\n");
}
if (argv[1] == argc[2]){
printf("Same\n");
} else{
printf("Different\n");
}
return 0;
} | Write the main() function of a program that takes exactly two arguments,
and prints one of the following:
- "Same\n" if the arguments are the same.
- "Different\n" if the arguments are different.
- "Invalid\n" if the program is called with an incorrect number of
arguments.
NOTE: Cut and paste these strings into your printf statements to
ensure that they will be exactly the strings above.
Your main function should return 0, regardless of what's printed. | null |
63c647dc5f004bf4898cf752 | 2023-02-02T21:58:23.670Z | Help Fix Code | v1 | null | int main(int argc, char ** argv){
if (argc != 3){
printf("Invalid\n");
return 0;
}
if (*argv[1] == *argv[2]){
printf("Same\n");
return 0;
} else{
printf("Different\n");
return 0;
}
}
| /*
Write the main() function of a program that takes exactly two arguments,
and prints one of the following:
- "Same\n" if the arguments are the same.
- "Different\n" if the arguments are different.
- "Invalid\n" if the program is called with an incorrect number of
arguments.
NOTE: Cut and paste these strings into your printf statements to
ensure that they will be exactly the strings above.
Your main function should return 0, regardless of what's printed.
*/ | null |
63c647dc5f004bf4898cf752 | 2023-02-02T21:59:54.178Z | Help Fix Code | v1 | null | int main(int argc, char ** argv){
if (argc != 3){
printf("Invalid\n");
return 0;
}
if strcmp(*argv[1], *argv[2]){
printf("Same\n");
return 0;
} else{
printf("Different\n");
return 0;
}
}
| Write the main() function of a program that takes exactly two arguments,
and prints one of the following:
- "Same\n" if the arguments are the same.
- "Different\n" if the arguments are different.
- "Invalid\n" if the program is called with an incorrect number of
arguments.
NOTE: Cut and paste these strings into your printf statements to
ensure that they will be exactly the strings above.
Your main function should return 0, regardless of what's printed. | null |
63c647dc5f004bf4898cf752 | 2023-02-02T22:00:33.781Z | Help Fix Code | v1 | null | int main(int argc, char ** argv){
if (argc != 3){
printf("Invalid\n");
return 0;
}
if !strcmp(*argv[1], *argv[2]){
printf("Same\n");
return 0;
} else{
printf("Different\n");
return 0;
}
}
| Write the main() function of a program that takes exactly two arguments,
and prints one of the following:
- "Same\n" if the arguments are the same.
- "Different\n" if the arguments are different.
- "Invalid\n" if the program is called with an incorrect number of
arguments.
NOTE: Cut and paste these strings into your printf statements to
ensure that they will be exactly the strings above.
Your main function should return 0, regardless of what's printed. | null |
63c647dc5f004bf4898cf752 | 2023-02-02T22:10:35.914Z | Help Fix Code | v1 | null | int main(int argc, char ** argv){
if (argc != 3){
printf("Invalid\n");
return 0;
}
if !strcmp(argv[1], argv[2]){
printf("Same\n");
return 0;
} else{
printf("Different\n");
return 0;
}
}
| Write the main() function of a program that takes exactly two arguments, and prints one of the following: - "Same\n" if the arguments are the same. - "Different\n" if the arguments are different. - "Invalid\n" if the program is called with an incorrect number of arguments. NOTE: Cut and paste these strings into your printf statements to ensure that they will be exactly the strings above. Your main function should return 0, regardless of what's printed. | null |
63c647dc5f004bf4898cf752 | 2023-02-02T22:12:00.702Z | Help Fix Code | v1 | null | int main(int argc, char ** argv){
if (argc != 3){
printf("Invalid\n");
return 0;
}
if strcmp(argv[1], argv[2]){
printf("Same\n");
return 0;
} else{
printf("Different\n");
return 0;
}
}
| Write the main() function of a program that takes exactly two arguments, and prints one of the following: - "Same\n" if the arguments are the same. - "Different\n" if the arguments are different. - "Invalid\n" if the program is called with an incorrect number of arguments. NOTE: Cut and paste these strings into your printf statements to ensure that they will be exactly the strings above. Your main function should return 0, regardless of what's printed. | null |
63c647dc5f004bf4898cf752 | 2023-02-02T22:20:17.173Z | Help Fix Code | v1 | null | int truncate(char s[], int n){
if(strlen(s) > n){
strncpy(s, s, n);
int removed = strlen(s) - n;
return removed;
} else {
return 0;
} | Write a function named truncate() that takes a string s and a
non-negative integer n. If s has more than n characters (not including the
null terminator), the function should truncate s at n characters and
return the number of characters that were removed. If s has n or
fewer characters, s is unchanged and the function returns 0. For example,
if s is the string "function" and n is 3, then truncate() changes s to
the string "fun" and returns 5. | null |
63c647dc5f004bf4898cf752 | 2023-02-02T22:23:59.449Z | Help Fix Code | v1 | null | int truncate(char s[], int n){
if(strlen(s) > n){
strncpy(s, s, n);
int removed = strlen(s) - n;
return removed;
} else {
return 0;
}
} | Write a function named truncate() that takes a string s and a non-negative integer n. If s has more than n characters (not including the null terminator), the function should truncate s at n characters and return the number of characters that were removed. If s has n or fewer characters, s is unchanged and the function returns 0. For example, if s is the string "function" and n is 3, then truncate() changes s to the string "fun" and returns 5. | null |
63c647dc5f004bf4898cf752 | 2023-02-02T22:28:48.178Z | Help Fix Code | v1 | null | int truncate(char s[], int n){
if(strlen(s) > n){
char copy[n+1];
strcpy(copy, s);
strncpy(s, copy, n);
int removed = strlen(s) - n;
return removed;
} else {
return 0;
}
}
| Write a function named truncate() that takes a string s and a
non-negative integer n. If s has more than n characters (not including the
null terminator), the function should truncate s at n characters and
return the number of characters that were removed. If s has n or
fewer characters, s is unchanged and the function returns 0. For example,
if s is the string "function" and n is 3, then truncate() changes s to
the string "fun" and returns 5. | null |
63c647dc5f004bf4898cf752 | 2023-02-02T22:29:28.244Z | Help Fix Code | v1 | null | int truncate(char s[], int n){
if(strlen(s) > n){
char copy[n+1];
strcpy(copy, s);
strncpy(s, copy, n-1);
s[n] = \0;
int removed = strlen(s) - n;
return removed;
} else {
return 0;
}
}
| Write a function named truncate() that takes a string s and a
non-negative integer n. If s has more than n characters (not including the
null terminator), the function should truncate s at n characters and
return the number of characters that were removed. If s has n or
fewer characters, s is unchanged and the function returns 0. For example,
if s is the string "function" and n is 3, then truncate() changes s to
the string "fun" and returns 5. | null |
63c647dc5f004bf4898cf752 | 2023-02-02T22:54:28.307Z | Help Fix Code | v1 | null | int strip_q_marks(char *string_ptr){
int n = strlen(*string_ptr);
int count = 0;
for (int i = n; i > 0; i--){
if (string_ptr[i] == "?"){
string_prt[i] = '\0';
count = count + 1;
} else {
return count;
}
}
}
| Complete this program by writing the function strip_q_marks that takes
a single string and returns an integer.
The function should modify the string to remove any trailing question marks
and return the number of question marks that were removed.
Note that you should put the command-line argument in double quotes when you
type it in. This prevents the shell from interpreting characters such as "?"
or a space as special characters, and passes them to the program as is. | null |
63c647dc5f004bf4898cf752 | 2023-02-02T22:59:13.570Z | Help Fix Code | v1 | null | int strip_q_marks(char *string_ptr){
int n = strlen(string_ptr);
int count = 0;
for (int i = n; i > 0; i--){
if (string_ptr[i] == '?'){
string_ptr[i] = '\0';
count = count + 1;
} else {
break;
}
}
return count;
} | Complete this program by writing the function strip_q_marks that takes
a single string and returns an integer.
The function should modify the string to remove any trailing question marks
and return the number of question marks that were removed.
Note that you should put the command-line argument in double quotes when you
type it in. This prevents the shell from interpreting characters such as "?"
or a space as special characters, and passes them to the program as is.
| null |
63c647dc5f004bf4898cf752 | 2023-02-02T23:03:17.758Z | Help Fix Code | v1 | null | int truncate(char s[], int n){
if(strlen(s) > n){
int removed = strlen(s) - n;
char copy[n+1];
strcpy(copy, s);
strncpy(s, copy, n);
return removed;
} else {
return 0;
}
} | Write a function named truncate() that takes a string s and a
non-negative integer n. If s has more than n characters (not including the
null terminator), the function should truncate s at n characters and
return the number of characters that were removed. If s has n or
fewer characters, s is unchanged and the function returns 0. For example,
if s is the string "function" and n is 3, then truncate() changes s to
the string "fun" and returns 5. | null |
63c647dc5f004bf4898cf752 | 2023-02-02T23:06:31.536Z | Help Fix Code | v1 | null | int truncate(char s[], int n){
if(strlen(s) > n){
int removed = strlen(s) - n;
char copy[n+1];
strcpy(copy, s);
strncpy(s, copy, n);
s[n] = '\0';
return removed;
} else {
return 0;
}
}
| Write a function named truncate() that takes a string s and a
non-negative integer n. If s has more than n characters (not including the
null terminator), the function should truncate s at n characters and
return the number of characters that were removed. If s has n or
fewer characters, s is unchanged and the function returns 0. For example,
if s is the string "function" and n is 3, then truncate() changes s to
the string "fun" and returns 5. | null |
63c647dc5f004bf4898cf752 | 2023-02-02T23:07:13.832Z | Help Fix Code | v1 | null | int truncate(char s[], int n){
if(strlen(s) > n){
int removed = strlen(s) - n;
char copy[n+1];
strcpy(copy, s);
strncpy(s, copy, n-1);
s[n] = '\0';
return removed;
} else {
return 0;
}
}
| Write a function named truncate() that takes a string s and a non-negative integer n. If s has more than n characters (not including the null terminator), the function should truncate s at n characters and return the number of characters that were removed. If s has n or fewer characters, s is unchanged and the function returns 0. For example, if s is the string "function" and n is 3, then truncate() changes s to the string "fun" and returns 5. | null |
63c647dc5f004bf4898cf752 | 2023-02-02T23:20:20.949Z | Help Fix Code | v1 | null | int truncate(char s[], int n){
if(strlen(s) > n){
int removed = strlen(s) - n;
s[n] = '\0';
return removed;
} else {
return 0;
}
}
| Write a function named truncate() that takes a string s and a
non-negative integer n. If s has more than n characters (not including the
null terminator), the function should truncate s at n characters and
return the number of characters that were removed. If s has n or
fewer characters, s is unchanged and the function returns 0. For example,
if s is the string "function" and n is 3, then truncate() changes s to
the string "fun" and returns 5. | null |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.