Hi all,

I am in need of some assistance with addresses & pointers as my dysfunctional professor is of no help. (I'll be as succinct as possible.)

First, the program must call three subroutines from main to fill "n" array elements with a given value; for instance, one must set value[n] equal to 0, another sets value[n] to 1, and a third sets value[n] to -1. This I was easily able to do.

Next, a fourth subroutine must be written to return the number of positive, zero, and negative values in the array with the prototype "void signs(int value[25], int n, int *np, int *nz, int *nn);", where "n" is the number of array values, "np" is the number of positive values, "nz" zero values, and "nn" negative values. My question is, what would be the best way to modify my program such that np, nz and nn will be incremented given the conditions of my "signs" subroutine below?

Here is my rough cut of the current assignment, which builds and runs in National Instruments CVI but does not increment np, nz, and nn.

#include <stdio.h>
#include <stdlib.h>

void zero(int value[25], int n){
	value[n] = 0;
}

void one(int value[25], int n){
	value[n] = 1;
}

void n_one(int value[25], int n){
	value[n] = -1;
}

void signs(int value[25], int n, int *np, int *nz, int *nn){
		if(value[n] == 0)
			nz++;
		else if (value[n] == -1)
			nn++;
		else if (value[n] == 1)
			np++;
}	

int main(){
	int n, value[25];
	int np = 0, nz = 0, nn = 0;
	for(n = 0; n < 5; n++)
		zero(value, n);
	for(n = 5; n < 20; n++)
		one(value, n);
	for(n = 20; n < 25; n++)
		n_one(value, n);
	for(n = 0; n < 25; n++)
		signs(value, n, &np, &nz, &nn);
		printf("ZEROS: %d\nPOSITIVES: %d\nNEGATIVES: %d\n", nz, np, nn);       
	return 0;
}

I would greatly appreciate any help received for this.

Recommended Answers

All 2 Replies

To increment (*ptr)++; Compile and run this little test program and you will see what it does.

#include <stdio.h>

void foo(int* n)
{
    (*n)++;
}

int main()
{
    int n = 0;
    int i;
    for(i = 0; i < 5; i++)
    {
        foo(&n);
        printf("%d\n", n);
    }
}

Ancient Dragon, you are quite the life saver. Thanks a million for jumping on this so fast; all I had to do was increment the pointers instead of actual variables.

-OTL

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.