help!

Write a code that declares an array of size 10 and then assigns
odd integers starting with 1 to each element. Then print the
array using pointer arithmetic. Hint: find pointer to the first
element and then increment.

i have no idea how to start this! someone please help!
closed account (iwTf92yv)
Whathave you got so far?
my teacher isnt the greatest and i dont even know where to start. i dont know how to make array go with pointer.

i know i need int a[10]
and
for(i=0;i<10;i++)
{
if(a[i]%2!==0)
cout<<a[i]
im getting nowhere with this. do u have any hints for me?
Let see.

1
2
3
4
5
6
7
8
9
int v[10], x=1;  //I putted v instead of a. Is not big deal
for(i=0; i<10; i++)
{    v[i] = x;         //x is an auxilliary variable because you cannot generate odd numbers using contors
      x = x+2; // odd numbers are: 1, 3, 5, 7, 9... so they can be generated by adding 2 to the previous number starting with 1.
}
//the printing part should be like this:
for(i=0; i<10; i++)
{   cout<<v[i]<<" ";
}
i need to use pointers.
i ended up with

{
#include <iostream>
using namespace std;

int main()
{

int a[10]={1,3,5,7,9,11,13,15,17,19};
int *i;


i = a;

int n;

for (n=0; n<10; n++)
{
cout << *i <<"\n";
i++;
}
system ("Pause");
return 0;
}



closed account (zb0S216C)
danush wrote:
1
2
3
int n;

for (n=0; n<10; n++)
(sic)

This ain't C, sunshine :)

danush wrote:
 
cout << *i <<"\n";
(sic)

Did you mean this:

 
cout << i[n] << '\n';


Wazzak
Last edited on
its c, im reading it right out of my book.

and so i meant *i, just like my notes say. *i is pointer
closed account (zb0S216C)
danush wrote:
its c (sic)

If this is C then your code won't compile at all.

danush wrote:
and so i meant *i, just like my notes say. *i is pointer (sic)

Dereferencing i produces the value of the first element of the array pointed to by i. The way I wrote prints the contents of the entire array, not only the first element. Have I missed the point?

wazzak
Last edited on
im reading it right out of my book.
and so i meant *i, just like my notes say. *i is pointer


Well then why are you telling us that? Have you compiled the code?
Topic archived. No new replies allowed.