Hey, Anyone willing to help with this program?

Write a c++ program that stores the following numbers in the array named miles:15,22,16,18,27,23, and 20. Have your program copy the data stored in miles to another array named dist, and then display the values in the dist array. Your program should use pointer notation when copying and displaying array elements.

This is what i have so far...

#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int i = 7;
int *dist;
int i = {15,22,16,18,27,23,20};

Idk if im doing this right...
Please help?
Thanks!
Arrays tutorial may be useful:
http://www.cplusplus.com/doc/tutorial/arrays/
as will the one on pointers:
http://www.cplusplus.com/doc/tutorial/pointers/
Arrays have the following format:

1
2
3
4

<array type><array name> [size of array];



If you initialise the array it would have this format

1
2
3

<array type><array name> [] = {array values};


For your code the array would look like this:

1
2
3

int miles [] = {15, 22, 16, 18, 27, 23, 20};


Now you want to copy this array to another array, so you could use a for loop:

1
2
3
4
5
6
7
8

int dist [i];

for (int count = 0; count < i; count++)
{
      dist [count] = miles [count];
}


Hope this helps.
I have to use C++ Pointers...
I'm lost with this program!
Well, as it says in the tutorial:
1
2
a[5] = 0;       // a [offset of 5] = 0
*(a+5) = 0;     // pointed by (a+5) = 0  

Quote:
These two expressions are equivalent and valid both if a is a pointer or if a is an array.

http://www.cplusplus.com/doc/tutorial/pointers/

In practice, for copying data using pointers, you would have something like this:
 
*p++ = *q++; 

(yes, that's in the tutorial too).
Last edited on
Topic archived. No new replies allowed.