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

What are keywords in the vbscript language?

0 Answers  


How can constants be declared in the vbscript language?

0 Answers  


What are class properties?

0 Answers  


Which conditional statement is the most convenient one to use in the case of multiple conditions in the vbscript language?

0 Answers  


Is vbscript a case-sensitive or case-insensitive?

0 Answers  






How many data types are supported in Vbscript?

3 Answers   Microsoft,


I want to import the sheet from the Excel to the Datatable using VB Script. I used the Syntax as 'Datatable.ImportSheet "Filename","SourceSheet","Destinat ionSheet" Ex: Datatable.ImportSheet "D:\Data1.xls","Sheet1","Global" Qtp producing run time error,How I can solve the problem

1 Answers  


Mention what is the use of option explicit in vbscript?

0 Answers  


write a vb script to generate hello 5 times using do until loop

4 Answers  


I have attended Anovatek Software QTP interview. They will give us computer and one web based application with QTP. We have to automate some records (already updated records or new records) using QTP Data driven testing. But we should use for loop? Can any one know how to do data driven testing using For loop?

0 Answers  


how to write codings in QTP using vb script. please help me. i am new to QTP. it is easy or very much tough. please tell me

3 Answers   TCS,


write a vb script to generate the following pattern ***** **** *** ** *

7 Answers   Blue Star,


Categories