Write a program to resize an array of 5 elements to 4 elements and display all the elements.
Answer / jon doe
C style answer:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
// create array with 5 elements
int *array5 = (int *) calloc(5, sizeof(int));
for(int i = 0; i < 5; ++i) {
array5[i] = rand();
}
// resize array
int *array4 = (int *) realloc(array5, 4 * sizeof(int));
for(int i = 0; i < 4; ++i) {
printf("%d) %d
", i, array4[i]);
}
free(array4);
return EXIT_SUCCESS;
}
C++ style answer:
int main(int argc, char *argv[]) {
// create array with 5 elements
int *array5 = new int[5]();
for(int i = 0; i < 5; ++i) {
array5[i] = rand();
}
// resize array
int *array4 = new int[4];
// copy array via loop. Alternative: use an array-copy function such as memcpy() for C or java.lang.System.arraycopy() for Java
for(int i = 0; i < 4; ++i) {
array4[i] = array5[i];
}
delete[] array5; // not used anymore
// print array
for(int i = 0; i < 4; ++i) {
printf("%d) %d
", i, array4[i]);
}
delete[] array4;
return EXIT_SUCCESS;
}
Is This Answer Correct ? | 0 Yes | 0 No |
what is event handling?
How can I get the value of an object property or variable in another frame?
Inorder to avoid Message box while writing script which alternative method can be used?
how to increasing the numbers in a given text box please write a vb script
Difference between Function and Sub routine?
How do you create a recordset object in VBScript?
write a vb script create 5 folders test1 test2 test3 test4 test5
how to increase the values in text box in a given text box increament by two values by clicking on button
Which operator can be used to do an xor operation in vbscript?
Mention the rules for using option explicit statement?
How are values assigned to string type and numeric type variables?
Explain about scrrun.dll?