Write a program to resize an array of 5 elements to 4 elements and display all the elements.
Answer Posted / 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 |
Post New Answer View All Answers
How to write functions and sub in vb script?
When does ‘on click of button’ event gets triggered in the vbscript language?
Explain about the functionality of vb script?
filter the array values without using filter function?
Which in-built function related to an array joins substrings into one string in the vbscript language?
Explain the tristate constants in vbscript?
Hello friends..... Can any give the methods for Ms-Access, and Mozilla firefox in Automation Object Model in QTP. Please give me currect answers... if you do not understand my question please don't give answers. Thanking you.
1) How can we use VB script in testing the application? 2) What all are the things(Software application to be installed in PC) we need to learn VBscript?
where can i learn VB scripint ?
How are arrays declared in the vbscript language?
Mention characteristics of sub procedures?
Explain the filter expression?
what is used of Property........End Property loop ? how to write the script for it?
How do you declare a variable in vbscript?
What is sql loader? Explain the files used by sql loader to load file?