a) (C language) Let’s consider an integer array of size 10. (10
marks, each part is 2 marks)
int a[10];
I. How would you assign a pointer, called pA, to store the address
of element 0 of this array? Write
the C code for your answer.
II. Using pA, how would you obtain the value of the next element
(element 1) of the array?
III. Explain in 2-3 sentences what the statement pA = a[1]; would
do.
IV. Is the statement a = pA; valid (would it cause compilation
errors)? How about a++;? Explain
briefly in 3-4 sentences.
V. Write a C code to print all the elements from this array.
Solution:
So, we have an array int a[10];
I. How would you assign a pointer, called pA, to store
the address of element 0 of this array? Write
the C code for your answer.
=> int *pA= &a[0];
II. Using pA, how would you obtain the value of the next element (element 1) of the array?
=> pA++; //by just incrementing the pointer by one the pointer will start to point at the next element value of the array a.
*pA is the value of the element at 1 position in the array a.
III. Explain in 2-3 sentences what the statement pA = a[1]; would do.
=> The statement will store the element value in the variable pA, but if pA is a pointer variable then there will be an error at compile time saying cannot assign int to an address.
IV. Is the statement a = pA; valid (would it cause
compilation errors)? How about a++;? Explain
briefly in 3-4 sentences.
=> Yes, the statement is valid.
because a is also a pointer pointing to the base element of array a which is a[0], and pA is also of pointer type.
a++ will assign the address of a[1] to the pointer variable a.
V. Write a C code to print all the elements from this array.
#include <stdio.h>
#include<conio.h>
int main()
{
int a[10]={0};
for(int i=0; i<10; i++)
{
printf("The element at %d index is= %d", i, a[i]); //printing the elements of the array
return 0;
}
Please give it a thumbs up if this helped you, also provide your valuable feedback.
a) (C language) Let’s consider an integer array of size 10. (10 marks, each part is...
the coding language is in c++ An array named testScores has already been declared. -size= 5 -data type = float - the array has already been initialized these values: 77, 88.5, 90, 93, 71.5 -Write one statement to declare a pointer named: ptr - in the same statement, assign the address to the testScores array to the pointer -write one statement to output the memory address of the array element at testScores[0] -Write a for loop to output the values...
Assume that integer array b[5] and integer pointer variable bPtr have been defined. Write a statement to set bPtr equal to the address of the first element in array b. Write a statement using pointer expression to reference the array element b[3]. please make sure answer should be detailed , correct and in C language.
using visual studio
2) For each of the following, write C++ statements that perform the specified task.. (Ref: classwork named pointer2) a) Declare a built-in array of type unsigned int called values with five elements, and initialize the elements to the even integers from 2 to 10. b) Declare a pointer vPtr that points to a variable of type unsigned int. c) Write two separate statements that assign the starting address of array values to pointer variable vPtr. d) Use...
Question 2 (a) Provide a single line of C++ code that will declare a variable that points to a string value, the variable is named x. [2 marks] (b) You have array c declared as string *c [5]; Give the single line of C+ + code that will assign the string pointer stored in variable x (from above) to the third element of the array c. [2 marks] (c) Write a for loop that initialises the contents of each element...
Write code to accomplish each of the following:Define a structure called Dove containing int variable dovNo and char array dovArr with values that may be as long as 25 characters (including the terminating null character).Define dove to be a synonym for the type struct Dove.Use Dove to declare variable a to be of type struct Dove, array b[ 10 ] to be of type struct Dove and variable ptr to be of type pointer to struct Dove.Read a dovNo and a dovArr from the keyboard into the individual members of variable a.Assign the member values of variable a to element 3 of array b.Assign the address...
B. Which statement would be used to define a to element integer array c? a. Array c = int[10]; b. C = int[10]; c. int Array c[10]; d. int c[10]; 17. Declare an Integer pointer variable named int_ptr. 18. Use the pointer created in question 17 and assign it the memory location of a variable named int_var. 19. Suppose you have an executable program named count that counts the characters in its input. Devise a command-line command using the count...
please answer all the questions.
2. (35 pts, 5 each) Consider the following array a. Write single-line statements to do the following double a[8] {0.0, = 10.0, 20.0, 30.0, 40.0), *p , ,g: Set the pointer p to point to the first element of the array - I ?-K-U) Set pointer q to point to the element with value 20.0 in the array as afn] with the correct n value. Use p above to triple the value stored in a[1]...
In need this in C language!
Write a program that declares a 40 element array of type double.
Initialize the array so that:
the first 10 elements are equal to the square of the index
variable
the next 10 each elements is equal to three times the index
variable
the next 10 each element is equal to the sum of an element in
the first 10 + an element in the second 10
the last 10 each element is equal...
(C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....
c++
Consider the following array definition: int values[5] = {4,7,6,8,2}; What does each of the following statements display? cout << values[4] << endl; cout< < (values[2] + values[3])<<endl; cout << ++values[1] << endl; marks is an integer array with 20 elements. Write a for loop that prints each element of the array. The arrays numberArray 1 and numberArray2 have 100 elements. Write code that copies the values in numberArrayl to numberArray2.