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


Please Help Members By Posting Answers For Below Questions

How to Convert Hex color code to color name in VB Script?

3927


If we take 2 strings as “good” and “bad” then what will ‘+’ and ‘&’ operators return?

530


We have 1 web page with names column. I am giving the Service Providers1,2,3.... @ that time dynamically some no of names are displaying in the webpage and The Pop up windows are opening(No.of Pop Up windows=No.of Names). The names may be diffar for each and every Service Provders (Dynamically) How can we handle the Dynamic values?

1750


What are the 2 ways to pass a value to the function?

509


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

12866






What is the use of option explicit in vbscript?

542


hi what is called GUI in QTP 8.2 and how can we test the apllication using GUI?

1782


Hellow friends, I am learning QTP,but here problem is VB script. please guide me how to learn VB script w.r.t QTP and if you know any books tell me or if you have any materials or any use full material or any else w.r.t QTP please post me p.p.sekhar

1960


Which loop is used in case of arrays in the vbscript language?

514


Explain about tristate constants?

579


Hello Friends , I am the new joinner of this website. I am working with Sapient ,Gurgoan I would like to ask few qurries regarding the QTP Tool of (Testing) Currently working on QTP Tool . I would like to no learn VB Script can u name some Books which i get in market. And a small issue in QTP I had 2 users right i have to login and send the document from this user to second user right. when i am send this doc some contendId number generates right. now i am loging into second user and i have to search for that contentId right i found it now i dont want to accept the document so i had an opption of check out ok i have to click on check out my QTP Code is like this Browser(" ").Page(" ").WebTable(" ").ChildItem (3,5,"Image",0).click Browser(" ").Page(" ").Link("ChecK Out").Click i new this code is perfectly right but when i am run the script i have to click on that particular contentId and click on check out but now the Problem is started the error is the document has been already checked out remeber every time my content id changes means it is the new contentId which is not been used atleast once Please help me out in this issue i am in big trouble

2021


if u ve resrevation for train and u get a number supose 1234xxxokie.after sucessul entering all required fields. now u put that number in search and want to chk wether these ar same or not if both ar same then its okie otherwise test fail.what would be the vb script code for it to compare these two values of different page.

1434


What are class events?

578


If a calulator having 3 buttons (of any number)in 3 of them one is not working properly due to which answer is wrong always. write a script to find out which button is not working properly ?

1550


What is Procedure or Subroutine in VB Script?

598