Passing Array values - pointer off set syntax issue

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1487 Views - Last Post: 07 July 2014 - 09:22 AM Rate Topic: -----

#1 dizzy2k11   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-June 14

Passing Array values - pointer off set syntax issue

Posted 06 July 2014 - 02:07 PM

Hello Everyone, currently doing an assignment where basically a user will enter an amount of elements, and the program should generate random numbers, depending on how many the user wants,

now, I have to write a function that calculates the average,
another function that calculates the highest the lowest.

right now im on the average function but im having problems. the program is not adding up the value of the random numbers. and i cannot use any [ ] whatsoever, only pointer off-set syntax
here the spec

6. Write a function that takes an array of doubles, and the size of the array. This function will calculate the
average of the values in the array, and return that value. Use pointer – offset syntax (no [ ] anywhere).

here the code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void getAverage(double *array, int size);

main(){

	double numbers;
	int numberAmount;
	int *a, i;

	srand(time(NULL));

	printf("Number of values to be entered:");
	scanf("%i", &numberAmount);
	printf("\n");

	printf("---------\n");
	a = (double*)calloc(sizeof(int), numberAmount);
	for (i = 0; i < numberAmount; i++){
		numbers = rand() % 100;
		printf("| %.2lf |\n", numbers);
	}
	printf("---------\n");

	getAverage(numbers, numberAmount);

	system("pause");
	return 0;
}

void getAverage(double *array, int size){
	int i;
	double totalAverage = 0.00; 
	
	for (i = 0; i < size; i++){
		totalAverage += *array;
	}
	totalAverage = totalAverage / size;

	printf("%.2lf", totalAverage);
}	



if i could get any pointer on how can i solve this issue, i think the other function wouldn't be a problem.
Thank you in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Passing Array values - pointer off set syntax issue

#2 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,941
  • Joined: 08-August 08

Re: Passing Array values - pointer off set syntax issue

Posted 06 July 2014 - 02:17 PM

	for (i = 0; i < size; i++){
		totalAverage += *array;
	}

How do you expect *array to change?
Was This Post Helpful? 0
  • +
  • -

#3 dizzy2k11   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-June 14

Re: Passing Array values - pointer off set syntax issue

Posted 06 July 2014 - 02:26 PM

*array should be the values from the main that were randomly chose from the user input, that for this exercise should be 10 values in total. those are the ones that i need calculated.

and I also just found another issue.

getAverage(numbers, numberAmount); 

argument of type "double" is incompatible with parameter of type "double *"

I looked into previous sections but i don't know how to fix, i don't see any problems. but the compiler wont let me go on
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,941
  • Joined: 08-August 08

Re: Passing Array values - pointer off set syntax issue

Posted 06 July 2014 - 02:36 PM

You've got lots of issues there. Tackle them one at a time. The value at *array will not change unless you change it.

Looking at it again, I think the best place to start is probably here:
    a = (double*)calloc(sizeof(int), numberAmount);

Does that compile on your system?
Was This Post Helpful? 0
  • +
  • -

#5 dizzy2k11   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-June 14

Re: Passing Array values - pointer off set syntax issue

Posted 06 July 2014 - 02:42 PM

ok, I just fixed some stuff, it seems to be working ... now my question is, how do I make the values from the main to go into the function?

im having issues there

Yes, actually the calloc does work properly I think, never used it before. this is the spec of the problem itself

1. Ask the user how many elements will be in their array.

2. Allocate an array dynamically using calloc.

3. Allocate enough space for that many doubles in an array. Don’t use any [ ].

4. Fill up the array with random values between 0.0 and 100.0 – use pointer-offset syntax – no [ ].

5. Print out the array elements in a table that has 10 columns.

6. Write a function that takes an array of doubles, and the size of the array. This function will calculate the
average of the values in the array, and return that value. Use pointer – offset syntax (no [ ] anywhere).
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,941
  • Joined: 08-August 08

Re: Passing Array values - pointer off set syntax issue

Posted 06 July 2014 - 02:49 PM

http://www.cplusplus...cstdlib/calloc/
How do your parameters match with those described in the manual?

This post has been edited by CTphpnwb: 06 July 2014 - 02:49 PM

Was This Post Helpful? 0
  • +
  • -

#7 dizzy2k11   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-June 14

Re: Passing Array values - pointer off set syntax issue

Posted 06 July 2014 - 03:23 PM

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

main(){

	int elements, *pNumbers, i;
	double values;

	printf("Please enter the amount of values");
	scanf("%i", &elements);

	srand(time(NULL));

	pNumbers = (double*)calloc(elements, sizeof(int));
	for (i = 0; i < elements; i++){
		values = rand() % 100;
		printf("%.2lf \n", values);
	}

	system("pause");
	return 0;
}


First of all, Thanks for your help everyone. I'm trying to learn and learn from my mistakes.
ok so, I went look at the tutorial that you posted CTphpnwb, and try to fix it. I hope this is correct.
so far it is generating the 10 double values that i need.

my problem is I have no idea how can I get these values into a function to calculate the average. with pointer offset and there shouldn't be any [ ] at all.
Was This Post Helpful? 0
  • +
  • -

#8 dizzy2k11   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-June 14

Re: Passing Array values - pointer off set syntax issue

Posted 06 July 2014 - 03:39 PM

No matter what I tried, I cant get the values to go into the array... and that's where im stuck right now.

Im sorry , into the function . the values randomly generated, need them to go into the function to find the average.
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,941
  • Joined: 08-August 08

Re: Passing Array values - pointer off set syntax issue

Posted 06 July 2014 - 03:45 PM

First, why sizeof(int)?
Next:
	for (i = 0; i < elements; i++){
		values = rand() % 100;
		printf("%.2lf \n", values);
	}


I see nothing there attempting to assign values to the array.

Oh, and why is pNumbers a pointer to int when you're using doubles?
Was This Post Helpful? 0
  • +
  • -

#10 dizzy2k11   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-June 14

Re: Passing Array values - pointer off set syntax issue

Posted 06 July 2014 - 03:58 PM

pNumbers = (int*)calloc(elements, sizeof(double)); 
-- I just noticed that, the sizeof is the type of each element so it would be double.

the spec
2. Allocate an array dynamically using calloc.

3. Allocate enough space for that many doubles in an array. Don’t use any [ ].

4. Fill up the array with random values between 0.0 and 100.0 – use pointer-offset syntax – no [ ].

If im understanding this right, which i don't think i am, pNumbers would be the array?
Im not sure how to do it without any [ ]

like declaring pNumbers as an array
Was This Post Helpful? 0
  • +
  • -

#11 dizzy2k11   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-June 14

Re: Passing Array values - pointer off set syntax issue

Posted 06 July 2014 - 04:05 PM

I had actually updates the program, and I fix those mistakes.. here the code.. Sorry about that

void getAverage(double *numbers, int size);

main(){

	int elements,  i;
	double values, *pNumbers;

	printf("Please enter the amount of values");
	scanf("%i", &elements);

	srand(time(NULL));

	pNumbers = (int*)calloc(elements, sizeof(double));
	for (i = 0; i < elements; i++){
		values = rand() % 100;
		printf("%.2lf \n", values);
	
	}

	getAverage(pNumbers, elements);

	system("pause");
	return 0;
}

void getAverage(double *numbers, int size){
	int i;

	for (i = 0; i < size; i++){
		printf("%.2lf \n",*numbers);
		
	}
}


Still, No idea how to assign the array without any []
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,941
  • Joined: 08-August 08

Re: Passing Array values - pointer off set syntax issue

Posted 06 July 2014 - 04:08 PM

Yes, pNumbers is the array. More precisely, it's a pointer to the beginning of the array. You'll want a pointer that can point to each element of the array: (beginning plus something...)
Was This Post Helpful? 0
  • +
  • -

#13 dizzy2k11   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-June 14

Re: Passing Array values - pointer off set syntax issue

Posted 06 July 2014 - 05:10 PM

would it be something like this?

*pNumbers (values + i)

inside of the for loop?


inside of the for loop? I look for it in my book but I do not really understand the concept.
Was This Post Helpful? 0
  • +
  • -

#14 dizzy2k11   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-June 14

Re: Passing Array values - pointer off set syntax issue

Posted 06 July 2014 - 05:17 PM

int *x;

BEGIN
{
  trace(x);
  trace(x + 1);
  trace(x + 2);
}


this is how my book is showing how to do it, but I'm getting no result if I put it inside of the for loop, or as a nested loop

trace would be pNumbers,
Was This Post Helpful? 0
  • +
  • -

#15 dizzy2k11   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 09-June 14

Re: Passing Array values - pointer off set syntax issue

Posted 06 July 2014 - 05:27 PM

	pNumbers = (int*)calloc(elements, sizeof(double));
	for (i = 0; i < elements; i++){
		values = rand() % 100;
		printf("%.2lf \n", values);

		*pNumbers = values;
		while (*pNumbers < elements){

			++*pNumbers;
		}
	
	}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2