Write a program to resize an array of 5 elements to 4 elements and display all the elements.



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

Post New Answer

More VB Script Interview Questions

write generic functions for webapplication?like generic function for webedit generic function for webbutton generic function for links

1 Answers  


Write a program to resize an array of 5 elements to 4 elements and display all the elements.

1 Answers  


what is diff between static and dynaic arrys?

0 Answers  


Consider there are objects in a webapp which are identified by QTP. Suppose you know only the logical name of a object(absolutely nothing else about it). Which approach will you take to find the "micClass" of the object(You can't use Object Spy, or can't add the object in repository)?

2 Answers   HCL,


What is the technology used by vb script?

0 Answers  






How to write VB Script for selecting a particular row in a WebTable?

1 Answers   Patni,


write a vb script to display the reverse of vbs

2 Answers  


Explain about the extension .hta?

0 Answers  


Mention what is variant in vbscript?

0 Answers  


how to declare a variable in vbscript using vbscript

4 Answers  


how to genarate a random numbers in vb?

2 Answers  


What is the purpose of folders object of scripting.filesystemobject class in vbscript?

0 Answers  


Categories