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
Explain different types of segment?
Explain the .wsf files?
Which operator can be used to do an xor operation in vbscript?
How many types of operators are available in the vbscript language?
Mention what is vbscript?
Can someone please tell me what poor design in a relational database (not the layout or style) is and how it can be avoided? PLEASE...im desperate.
Hi Friends Rajendra this is bhavani prasad, iam working Hyderabad. i faced one problem with qtp recording mode i.e in my application there is 100 records first we click the first record that record will be jumped to next session and 99 records will there stop the recording and run the same script .Run this script qtp does not identify the records. So plz tell me what is the solution.
What is the use of the instr function?
while using Keyward driven framework in QTPif new requirements are added how to manage it...plz ans
How will you convert a given number to long in vbscript?
How will you check that a variable is an array in vbscript?
What is the use of the formatdatetime function in the vbscript language?
how to set one column as primary key in QTP and fetch values accordingly
How will you get the smallest subscript of an array in vbscript?
does anyone have qtp11.0 license key.Please sendit to my mail id-rrvv2011@gmail.com...Thanks