Question

Pointer Comparisons Assignment Write a small program called PointerComparisions LastName.cpp that • Declares an array of floa

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


/*C++ program that demonstrates the floatArray and the pointer ,floatArrayPointer that points to the floatArray
and then display the values of floatArray from first to last and last to first using for loop. Then check the values of floatArray
and the pointer pointed by floatArrayPointer.
*/
//PointerComparisionsLastName.cpp
#include<iostream>
using namespace std;
int main()
{
   const int size=4;
   //float array with 4 elements
   float floatArray[]={15.5,14.5,25.5,12.5};

   //print the values and its memory addresses
   //using for loop from first to last
   cout<<"floatArray values from frist to last\n";
   for(int index=0;index<size;index++)
   {
       cout<<"Value : "<<floatArray[index]<<endl;
       cout<<"Address : "<<&floatArray[index]<<endl;
   }

   cout<<"\n\n";

   //print the values and its memory addresses
   //using for loop from last to first
   cout<<"floatArray values from last to first\n";
   for(int index=size-1;index>0;index--)
   {
       cout<<"Value : "<<floatArray[index]<<endl;
       cout<<"Address : "<<&floatArray[index]<<endl;
   }

   cout<<"\n\nChecking pointer array, floatArrayPointer values with floatArray values.\n";
   //create a pointer ,floatArrayPointer that points to floatArray
   float *floatArrayPointer=floatArray;


   for(int index=0;index<size;index++)
   {
       if(*(floatArrayPointer+index)==floatArray[index])
           cout<<*(floatArrayPointer+index)<<" = "
                   <<floatArray[index]<<endl;
       else
           cout<<*(floatArrayPointer+index)<<"not equal to "
                   <<floatArray[index]<<endl;
   }

   system("pause");
   return 0;
}

sample output:

floatArray values from frist to last Value : 15.5 Address : 005AF708 Value : 14.5 Address : 005AF70C Value : 25.5 Address : 0

Add a comment
Know the answer?
Add Answer to:
Pointer Comparisons Assignment Write a small program called PointerComparisions LastName.cpp that • Declares an array of...
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
  • 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...

  • Submit two C++ files: 1. Write a C++ program that declares an array alpha of 50...

    Submit two C++ files: 1. Write a C++ program that declares an array alpha of 50 components of type double. Initialize the array so that the first 25 components are equal to the square of the index variable, and the last 25 components are equal to three times the index variable. Output the array so that 10 elements per line are printed. 2. Write a program that prompts the user to input a string and outputs the string in uppercase...

  • Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You...

    Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You can initialize the variables to any legal value of your choosing. Next, declare and initialize a pointer variable to each of the variables Using printf), output the following: The address of each of the first three variables. Use the "Ox%x" format specifier to print the addresses in hexadecimal notation The value of each variable. They should equal the value you gave...

  • C++ Program - Arrays- Include the following header files in your program:     string, iomanip, iostream Suggestion:...

    C++ Program - Arrays- Include the following header files in your program:     string, iomanip, iostream Suggestion: code steps 1 thru 4 then test then add requirement 5, then test, then add 6, then test etc. Add comments to display assignment //step 1., //step 2. etc. This program is to have no programer created functions. Just do everything in main and make sure you comment each step. Create a program which has: 1. The following arrays created:                 a. an array...

  • Write a c program that finds the uncommon elements from two array elements using pointers only...

    Write a c program that finds the uncommon elements from two array elements using pointers only . For example : Enter the length of the array one : 5 Enter the elements of the array one: 9 8 5 6 3 Enter the length of the array two: 4 Enter the elements of the array two: 6 9 2 1 output: 8 5 3 2 1 void uncommon_ele(int *a, int n1, int *b, int n2, int *c, int*size); The function...

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

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

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

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

  • c++ please no global varaible write a value returning function input data to dynamically allocate a...

    c++ please no global varaible write a value returning function input data to dynamically allocate a short array of size elements and store the input data entered from the disk file into array.Return the base address of the array allocated.Read the data from the text file Hello.txt and store the data in reverse order in the array.the size of the data set will be in the file size 10 and the data was 1 2 3 4 5 6 7...

  • PROGRAM DESCRIPTION: In this assignment, you will be creating a memory matching game in C++. In t...

    c++ PROGRAM DESCRIPTION: In this assignment, you will be creating a memory matching game in C++. In this game, the user will need to match up the pairs symbols A,B,C,D,E on a 4x4 array. For example, the array could be initialized like the following: In this case, X represents an empty slot. The goal is for the user to match the A to the A, the B to the B, etc, until all pairs are matched up to win the...

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