Question

(15 pts) Using the following example, int a, p=&a; *p = 20; Give the two major...

  1. (15 pts) Using the following example,
    int a, p=&a;
    *p = 20;
    Give the two major differences of * and & in the above example.
  2. (35 pts) Do the following in a program segment step by step. The next step uses the results of the previous step and earlier, using a single statement only and none for manual work.
    1. Declare an array called a of float of size 10 and initialize it to
      2.5, -3.3, 5.9, 1.0, 3.5, 4.3, -7.3, -3.6, 6.1, 2.6

    2. Declare a point p and initialize it to point to a[2].

    3. Declare a point q and initialize it to point to a[8].

    4. Using pointer arithmetic, print the value of a[5] as a line using p but do not change p.

    5. Increment the pointer q by 1 using ++.

    6. Manually give the value of p-q. Note: do not use fprint to print it.

    7. Manually give the value of *q - *p.

  3. (20 pts) Write a function readBetween that takes three parameters, a float array as a pointer p as the first parameter, and two int indices startIdx and endIdx. The function readBetween reads the content of the array between startIdx and endIdx, without using additional local valuables.
  4. (20 pts) Write a function called tripleOdds that takes two parameters, an array of double pointed to by pointer p and endIdx of int type. The function tripleOdds triples (multiply by 3) every other value in the array (values at 1, 3, 5, …).
  5. (10 pts) Do the following steps, one step after another.
    1. Using constant directive #define, define string STR_LEN as 50.

    2. Define string day[5][STR_LEN] and initialize each to “Monday” to “Friday” using five statements, where day[0] is Monday, day[1] is Tuesday, and so on.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

  1. int a, p=&a;
    *p = 20;
    two major differences of * and & in the above example.
  2. a) * is used to get value at an address whereas & is used to represent an address.
  3. b) * is used for pointer variable declaration and & is used for reference variables.

a.

float a[] = {2.5, -3.3, 5.9, 1.0, 3.5, 4.3, -7.3, -3.6, 6.1, 2.6};

b.

float *p = a[2];

c.

float *q = a[8];

d.
printf("%d",*(p+3));

e.
q++;

f.
p-q = 6

g.
*q -*p = 6.1 -5.9 = 0.2

3.

void readBetween(float *p,int startIdx,int endIdx) {

fot(int i = startIdx;i<endIdx;i++)

printf("%f",*(p+i));

}

4.

void tripleOdds(double *p,int endIdx){

for(int i = 0;i<endIdx;i++)

*(p+i) = 3* *(p+i);

}

5

a.
#define STR_LEN 50

b.

string day[5][STR_LEN];

day[0][STR_LEN] = "Monday";

day[0][STR_LEN] = "Tuesday";

day[0][STR_LEN] = "Wednesday";

day[0][STR_LEN] = "Thursday";

day[0][STR_LEN] = "Friday";

Do ask if any doubt. Please upvote.

Add a comment
Know the answer?
Add Answer to:
(15 pts) Using the following example, int a, p=&a; *p = 20; Give the two major...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C language 3. (10 pts) Write a function readSegment that takes three parameters, a float array...

    C language 3. (10 pts) Write a function readSegment that takes three parameters, a float array as a pointer p as the first parameter, and two int indices startldx and endldx. The function readSegment reads the content of the array between startIdx and endldx, without using additional local variables. 4. (10 pts) Write a function called doubleOdds that takes two parameters, an array of double pointed to by pointer p and endldx of int type. The function doubleOdds doubles (multiply...

  • 1. (40 pts) Consider the following function. Complete the function that does the following: (a) Take...

    1. (40 pts) Consider the following function. Complete the function that does the following: (a) Take three arguments, int array, int as the length of the array, and int *max. (b) Find the maximum value and the minimum value in the array. (c) The pointer max points to the maximum value. (d) Return the pointer that points to the minimum value max)( findMinAndMax(int al l, int length, int int maxValue a[0], minValue al0 int int maxldx 0, minldx 0; for...

  • Using Swift playground and / or the command line for macOS (open Xcode, create a new...

    Using Swift playground and / or the command line for macOS (open Xcode, create a new Xcode project, macOS, command line tool), practice the following exercises: Exercise: Swift Variables Declare 2 variables/constants with some random values and any name of your choice Considering these 2 variables, one as a value of PI and other as a radius of circle, find the area of circle Print the area of circle Declare a string variable explicitly with value “Northeastern”. Declare a string...

  • Create 3 variables, (int, chart and float) and give give each of them some value. Print...

    Create 3 variables, (int, chart and float) and give give each of them some value. Print out the values. Then create a pointer for each variable and initialize the pointers. Then make them point to the memory address of the corresponding variables. Print out the pointers (=memory addresses where the pointers point to, use %p). Finally, print out the values that are in the memory locations where the pointers point to(so print out the values using the pointers, not the...

  • Assuming the following class declaration: class   Course {        private:               string grade_;        &n

    Assuming the following class declaration: class   Course {        private:               string grade_;               int       units_;         public:              Course ( );              Course ( string grade, int units);               ~Course ( );               string get_grade ( ) const ;               int       get_units ( ) const; }; NOTE: Class definitions are not provided but you may assume they're properly defined. Variable names used in your answers must follow our stated naming convention especially for pointer names. Write a fragment of...

  • using visual studio 2) For each of the following, write C++ statements that perform the specified...

    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...

  • Using C code 1. Consider a hash table that is implemented using the following struct definitions....

    Using C code 1. Consider a hash table that is implemented using the following struct definitions. #define NUMBUCKETS 10 typedef struct _HTE { int Key; int Value; struct _HTE *Next; } HashTableEntry; typedef struct _HT { HashTableEntry *Buckets[ NUMBUCKETS ]; unsigned int Size; } HashTable; a.) Complete the following function, which takes a pointer to a HashTableEntry, which is the head of a linked list of entries, and an integer x. It returns the first entry in the list whose...

  • C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array...

    C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array named f, and one double array named d, each of size M. (b)Declare array x with N of those structs. (c)Write a void function to traverse array x (using a pointer) assigning to each element in each array d (in each struct in array x) the sum of the corresponding elements in arrays i and f (in the same struct). Use 3 pointers (of...

  • Code in C language ADT: typedef struct{ int ID; float salary; int age; }Employee; ...

    code in C language ADT: typedef struct{ int ID; float salary; int age; }Employee; Specification: In this lab, five functions need to be implemented using the given ADT. 1. Employee* readRecord(FILE*) This function receives a FILE pointer created before. It reads a line from the provided csv file, and creates an Employee struct pointer with the information from that line then returns the pointer back to the calling function. Each line in the provided csv file contains the id, salary,...

  • State true or false: (6 x 2 = 12 pts) Using -- with a reverse iterator...

    State true or false: (6 x 2 = 12 pts) Using -- with a reverse iterator moves it from the back toward the front of a list b) The body of a do-while loop is always executed at least once. c) If p points to the array element a[j], then p + i points to a[i+j]. d) Any number of pointer variables may point to the same object. In terms of both execution speed and memory usage usually iterative solutions...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT