pointers homework

array of doubles and array of pointers

Page 1 of 1

14 Replies - 2588 Views - Last Post: 09 October 2005 - 05:27 PM Rate Topic: -----

#1 jsbeckton  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-October 05

pointers homework

Posted 08 October 2005 - 05:32 AM

have a programming assignment that has had me baffeled all week here is the assignment:

"Construct a program that declares a size 10 array of doubles and an array of pointers to doubles.

In a for loop, using pointer arithmetic to increment, initialize the integer array from 1.0 to 10.0 and in same loop initialize the pointer array to point to the array elements holding the data.

Create a print function that accepts the pointer array and its size as an argument. The function should, using square bracket notation, print out the contents of the pointer array and the values in the double array.

An possible example of output:

0x001f20 1.0
0x001f28 2.0

etc."


this is what I have so far but its riddeled with errors

#include <stdio.h>
#define SIZE 10;



int main()
{
	
	double values[10];	
    double *ptr[10];
    int i;

   

	for ( i = 0; i < SIZE; i++ ) {
  (*(values + i) = i + 1;);
  *(ptr    + i) = values + i;
  
	}
	
    print(ptr, SIZE );

    void print(double *ptr[SIZE], int size);

	
  

   printf("%p %.1f\n", (void*)ptr[i], *ptr[i]);


	}
 


I would really appreciate any help fixing this because its due monday.

Is This A Good Question/Topic? 0
  • +

Replies To: pointers homework

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: pointers homework

Posted 08 October 2005 - 07:42 AM

I'm sorry, I'm not near a compiler (gasp!)...can you post the errors you're getting?
Was This Post Helpful? 0
  • +
  • -

#3 jsbeckton  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-October 05

Re: pointers homework

Posted 08 October 2005 - 02:29 PM

these are my errors:

Compiling...
CH_7helped.c
CH_7helped.c(16) : error C2143: syntax error : missing ')' before ';'
CH_7helped.c(16) : error C2059: syntax error : ')'
CH_7helped.c(16) : error C2143: syntax error : missing ';' before '{'
CH_7helped.c(17) : error C2143: syntax error : missing ')' before ';'
CH_7helped.c(17) : error C2059: syntax error : ')'
CH_7helped.c(22) : warning C4013: 'print' undefined; assuming extern returning int
CH_7helped.c(22) : error C2143: syntax error : missing ')' before ';'
CH_7helped.c(22) : error C2059: syntax error : ')'
CH_7helped.c(24) : error C2143: syntax error : missing ';' before 'type'
CH_7helped.c(24) : error C2143: syntax error : missing ';' before ']'
CH_7helped.c(24) : error C2059: syntax error : ')'
Error executing cl.exe.

CH_7helped.exe - 10 error(s), 1 warning(s)
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: pointers homework

Posted 08 October 2005 - 02:50 PM

There's an extra semi colon in the first line of the for loop...that will cause a little hiccup...take that out and recompile, see what we have.

Also, is this
    void print(double *ptr[SIZE], int size);
  printf("%p %.1f\n", (void*)ptr[i], *ptr[i]);


supposed to be a function definition? If so, you'll have to define it outside the main function, like so
#include <stdio.h>
#define SIZE 10;

void print(double *ptr[SIZE], int size);

int main()
{
   double values[10];
   double *ptr[10];
   int i; 
   for ( i = 0; i < SIZE; i++ ) {
      (*(values + i) = i + 1);
      *(ptr    + i) = values + i;
   }
   print(ptr, SIZE );
}

void print(double *ptr[SIZE], int size)
{
  printf("%p %.1f\n", (void*)ptr[i], *ptr[i]);
}


Also note that the header for the function does not require a semi colon when it is defined, only when it is declared.
Was This Post Helpful? 0
  • +
  • -

#5 jsbeckton  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-October 05

Re: pointers homework

Posted 08 October 2005 - 03:31 PM

i dont see the extra semi colon in the first line of the for loop?
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: pointers homework

Posted 08 October 2005 - 03:33 PM

I took it out in the code I cleaned up...this is your code from above
(*(values + i) = i + 1;);


There are two semi colons...I removed the one from in front of the closing bracket.
Was This Post Helpful? 0
  • +
  • -

#7 jsbeckton  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-October 05

Re: pointers homework

Posted 08 October 2005 - 03:38 PM

i compiled it as you had it and here are the errors:

Compiling...
CH_7helped.c
CH_7helped.c(13) : error C2143: syntax error : missing ']' before ';'
CH_7helped.c(13) : error C2143: syntax error : missing ')' before ';'
CH_7helped.c(13) : error C2059: syntax error : ']'
CH_7helped.c(13) : error C2059: syntax error : ')'
CH_7helped.c(20) : error C2143: syntax error : missing ')' before ';'
CH_7helped.c(20) : error C2059: syntax error : ')'
CH_7helped.c(20) : error C2143: syntax error : missing ';' before '{'
\CH_7helped.c(24) : warning C4020: 'print' : too many actual parameters
CH_7helped.c(24) : error C2143: syntax error : missing ')' before ';'
CH_7helped.c(27) : error C2143: syntax error : missing ']' before ';'
CH_7helped.c(27) : error C2143: syntax error : missing ')' before ';'
CH_7helped.c(27) : error C2059: syntax error : ']'
CH_7helped.c(27) : error C2059: syntax error : ')'
Error executing cl.exe.
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: pointers homework

Posted 08 October 2005 - 04:14 PM

You've got a semi colon after the define directive...that should not be there. In the function prototype, do not specify the SIZE paramter...it should look like this:
void print(double *ptr[], int size);


Same with the definition.

The print function itself is using a variable i, but that variable is not declared in the function, nor is it passed in.

I'm not near a compiler, so I may be missing some things...these are just observations...I'll compile it later tonight.
Was This Post Helpful? 0
  • +
  • -

#9 jsbeckton  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-October 05

Re: pointers homework

Posted 08 October 2005 - 04:17 PM

should I move the function inside the for loop where i is declared?
Was This Post Helpful? 0
  • +
  • -

#10 jsbeckton  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-October 05

Re: pointers homework

Posted 08 October 2005 - 05:31 PM

Can I call the print function while inside the for statement? I tried moving it in but it still says that i is a undeclared identifier even though it is used already in the for statement.

This post has been edited by jsbeckton: 08 October 2005 - 05:45 PM

Was This Post Helpful? 0
  • +
  • -

#11 jsbeckton  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-October 05

Re: pointers homework

Posted 08 October 2005 - 05:46 PM

I can feel that its getting close
#include <stdio.h>
#define SIZE 10

void print(double *ptr[], int size);

int main()
{
  double values[10];
  double *ptr[10];
  int i; 

  for ( i = 0; i < SIZE; i++ )
  {
      *(values + i) = i + 1;
      *(ptr    + i) = values + i;

  print(ptr, SIZE );

  }
 
  return 0;

} 

 void print(double *ptr[], int size) {

printf("%p %.1f\n", (void*)ptr[i], *ptr[i]);

} 


Was This Post Helpful? 0
  • +
  • -

#12 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: pointers homework

Posted 08 October 2005 - 06:56 PM

It is still undeclared because it is undeclared in the print function...that is where it's being used. I'd suggest since your print function is only one line, and if you want to print every time through the loop, that you use this:

#include <stdio.h>
#define SIZE 10

int main()
{
 double values[10];
 double *ptr[10];
 int i;

 for ( i = 0; i < SIZE; i++ )
 {
     *(values + i) = i + 1;
     *(ptr    + i) = values + i;
     printf("%p %.1f\n", (void*)ptr[i], *ptr[i]);

 }

 return 0;

}


if you only want to print out the last value of i, put it just outside the for loop.
Was This Post Helpful? 0
  • +
  • -

#13 jsbeckton  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-October 05

Re: pointers homework

Posted 08 October 2005 - 08:21 PM

the directions to the assignment say to create a print function. I think that he wants us to get used to callling the function. I know that that method (above) will print out the correct results, however as I understand it thats not how he wants to do it I guess. Am I reading the question correctly?
Was This Post Helpful? 0
  • +
  • -

#14 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: pointers homework

Posted 09 October 2005 - 06:49 AM

Sounds like it...in that case, just modify what you send...each time through the loop, call the function, but only send it the elements you're currently on.

Conversely, send it the value of i as a paramter as well, and re run the loop in the print function.
Was This Post Helpful? 0
  • +
  • -

#15 jsbeckton  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-October 05

Re: pointers homework

Posted 09 October 2005 - 05:27 PM

Success!!!!!!!!!!!!!!!!!

Thank you very much for your help. I got it to work and more importantally I understand how it works. However there is one piece that still has me a bit confused and that is the actual print command:
printf("%p %.1f\n", (void*)ptr[i], *ptr[i]);


more particularlly the part about:
(void*)ptr[i]


what exactally does this mean and is there another way to write it because it just looks weird to me. Is there a way to write it using the & opperator?

Thanks once again for all of your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1