Question

Rewrite the program you submitted for A8 to use pointers, pointer notation and arithmetic, etc. as...

  1. Rewrite the program you submitted for A8 to use pointers, pointer notation and arithmetic, etc. as follows:
    • If you did not complete A8, you will need to start with those instructions, and then, change your code based on the instructions here
    • The main() function should:
      • Create a fixed or dynamic array, e.g., double grade[SIZE] or double *grade = malloc(...) or calloc(...)
      • Concerning the function call for getData(), pass the address of the array and its size to pointers; depending on how you change your program, it may or may not return something, but if it does, it should return an address. i.e., a pointer

Here is

      • Concerning the function call for displayData(), pass the address of the array and its size to pointers, and return nothing
      • Use the final for loop, which displays the contents of the array again without using displayData(), to demonstrate one of the ways, you can use pointers to manipulate an array; see below for more information
    • The for loops, inside of the getData() and displayData() functions, should use different ways to demonstrate how to manipulate an array, e.g., p < q vs. p + i < p + size, or whatever; see the Notes - Pointers and second video in C10, if necessary
    • Your grade will be better, if your code thoroughly demonstrates using pointers, and pointer notation and arithmetic, and follows the Code Conventions, etc.
  1. Make sure your program thoroughly follows the Code Conventions, including tabs, not multiple spaces, appropriate comments at the top, PPR comments above the function definitions, heading type comments and single blank lines throughout your code to group and organize your code, e.g., // Prototypes, // Input, // Output, etc

Here is Assignment A8 for reference.

  1. Above main(), where appropriate, create a constant for the size of the array in step 2.
  2. In main(), create an array called grade using the constant from step 1 and initialize it to zero.
  3. Below main(), create a function called getData(), which takes and processes an array called array along with its size, and fills it with numbers given by the user. Use an array reference, not a pointer, and do NOT use a constant for this function. Also, the function does not return anything.
  4. In main(), call getData() passing the grade array and its size to the function.
  5. Below main(), create a function called displayData(), which takes and processes an array called array along with its size, and retrieves and displays the values from the array to the screen formatted professionally with decimal points aligned if appropriate. Use an array reference, not a pointer, and do NOT use a constant for this function. Also, the function does not return anything.
  6. In main(), call displayData() passing the grade array and its size to the function.
  7. In main(), using a for loop, display the data in the grade array. Do NOT use the function displayData() for this step.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program code to copy

//header files
#include <iostream>
#include <iomanip>
using namespace std;

//*******************************************************************************************************
//function prototypes
void getData(int *, const int);
void displayData(int *, const int);

//*******************************************************************************************************
//main function
int main()
{
   //constant variable
   const int SIZE = 10;

   //declare local variable
   int grades[SIZE];

   int * ptData = grades;

   //calling a function
   getData(ptData, SIZE);

   displayData(ptData, SIZE);

   //return value          
   return 0;
}

//*******************************************************************************************************
// pass the address of the array and its size to pointers
void getData(int * data, const int SIZE)
{
   cout << "Please enter grades for the 10 assignments." << endl;
  
   //using for loop for accessing a input data
   for (int i = 0; i < SIZE; i++)
   {
       do
       {
           //asking user to enter a grade
           cout << "Grade " << i + 1 << ": ";
           cin >> *(data + i);

           // if the grade value is less than zero or greater than 100 then it will display error message using if loop
           if (*(data + i) < 0 || *(data + i) > 100)
               cout << "ERROR: Grades cannot be less than 0 or more than 100" << endl;
       } while (*(data + i) < 0 || *(data + i) > 100);
   }

   cout << endl;
}

//*******************************************************************************************************
// display all the grades which the user enters
void displayData(int * data, const int SIZE)
{
   cout << "All grades entered:" << endl;

   //using for loop ,which displays the contents of the array
   for (int i = 0; i < SIZE; i++)
   {
       cout << left << setw(5) << *(data + i);
   }
  
   cout << endl << endl;
}

===================================================================================

Program Screenshot

===================================================================================Sample Output

Add a comment
Know the answer?
Add Answer to:
Rewrite the program you submitted for A8 to use pointers, pointer notation and arithmetic, etc. as...
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
  • Here's an assignment that'll give you some practice with pointers. All you have to do is...

    Here's an assignment that'll give you some practice with pointers. All you have to do is write a program that allows the user to populate an array of doubles (5 values) by calling the function InitArray. After the user has entered all the values, then call the function DispRevArray to display the array in reverse. Main will create and allocate space for an array of type double for 5 elements. Then you will create and allocate a pointer that will...

  • Write in C code: Define an array f=[23,-12,34,-56,-10,12,19], using call-by-reference (use pointers), to pass the array...

    Write in C code: Define an array f=[23,-12,34,-56,-10,12,19], using call-by-reference (use pointers), to pass the array to a function, named Summation. In main: Define array, pass array, print out the array and the result returned from Summation on screen. In function Summation: 1)take array and size from main 2) sum each element according to the following rules 3) if the value of f(i) is positive, add 2*f(i) to the sum 4)if the value of f(i) is negative, add -3*f(i) to...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to...

    In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to make the displayAnimal function anim parameter as an object variable, not a reference variable. Run the code and examine the output. What has changed? Polymorphic behavior is not possible when an object is passed by value. Even though printClassName is declared virtual, static binding still takes place because anim is not a reference variable or a pointer. Alternatively we could have used an Animal...

  • please use c ++ language and write the comment too. thanks Pointer Arithmetic Write a function...

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

  • C programming. 1.Create a program that does the following - Creates three pointers, a character pointer...

    C programming. 1.Create a program that does the following - Creates three pointers, a character pointer professor, and two integer pointers student_ids, grades - Using dynamic memory, use calloc to allocate 256 characters for the professor pointer - Prompts the professor for their name, and the number of students to mark. - Stores the professor’s name using the professor pointer and in an integer the number of students to mark. - Using dynamic memory, use malloc to allocate memory for...

  • C programming 3. (40 pts) Define an array fof size 10, using call-by-reference (that is, use...

    C programming 3. (40 pts) Define an array fof size 10, using call-by-reference (that is, use of pointers), to pass the array to a function, named summation In main: define array, pass array, print out the array and the results on screen In function summation: take array from main and sum the array using the formula below: 10 fO 2 246 NOTE: you must use call-by-reference (the pointer) to call functions, as required; otherwise, no credits will be given.

  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • c++ if you answer the question please write it line by line and not in paragrapth...

    c++ if you answer the question please write it line by line and not in paragrapth form. Write a C++ program that uses a structure named Movie to store the following. a. Title b. Director c. Year Released d. Running time (in minutes) e. Cost of production f. Revenue (first year). Create an array of type Movie of size 3. In your main program should then have in a loop (3 times) called a function called getData() and the purpose...

  • Question 1: Pointers You are given the following C code and memory diagram. The “contents” column...

    Question 1: Pointers You are given the following C code and memory diagram. The “contents” column of the memory diagram shows the value of the variable. Update the “contents” column of the memory diagram for each assignment in main(). long *pt; long data;    long buffer[4]; void main(void){   pt = &buffer[1];   *pt = 1234;   data = *pt; } address      contents   variable 0x20000000 0x00000000    pt 0x20000004 0x00000000    data 0x20000008 0x00000000    buffer[0] 0x2000000C 0x00000000    buffer[1] 0x20000010 0x00000000    buffer[2] 0x20000014 0x00000000    buffer[3]...

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