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...
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...
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...
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.
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...
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...
(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....
please use c ++ language and write the comment too. thanks Pointer Arithmetic Write a function that passes an array address to a function as a pointer. Using pointer arithmetic, the function determines the average, high, and low value of the array. Your function should have this header: void pointerArithmetic(int *array, int size, double &average, int &high, int &low) Parameter size is the number of elements in array. You may not use the subscript notation [] inside your function –...
Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...
Suppose v is an array with 100 int elements. If 100 is assigned to v[100], what happens? Show the code. An array of 1000 integers is declared. What is the largest integer that can be used as an index to the array? Shows the code. Consider the declaration: int v[1]; What is the index of the last element of this array? Declare an array named a of 10 int elements and initialize the elements (starting with the first) to the...