C Programming Help- NEWER QUESTION ADDED TO BOTTOM

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
I have to get the number of elements in an array using a function. This is what I have, and I have tried many variations and changing things around but I cannot get it to return the correct number. Point me in the right direction please?

I only attached the parts that are relevant to my question.

NEW QUESTION: How can I get it to loop through an array until it reaches the value of -1? Can I do while(i!=-1)? I think a for statement afterwards but what do I put in the middle? for(j=0,?,j++)?
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
C doesn't keep track of the size of arrays for you. You have to keep an integer along side it.
 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
Originally posted by: kamper
C doesn't keep track of the size of arrays for you. You have to keep an integer along side it.


Okay, I guess I dont have to have sizeof. I just thought I might have to use it since it was in the chapter we are covering. But, I guess if I can do without it why use it, its not in the requirements.
thanks
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Just for explanation's sake, what your code above was doing was dividing the size of an integer pointer (probably 4) by the size of an integer (probably 4) so I'm guessing you were getting 1 back all the time. If you haven't done pointers yet, they're just a memory address pointing to where you've got your integers. An array is nothing more than a pointer, hence the lack of length information.
 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
Originally posted by: kamper
Just for explanation's sake, what your code above was doing was dividing the size of an integer pointer (probably 4) by the size of an integer (probably 4) so I'm guessing you were getting 1 back all the time. If you haven't done pointers yet, they're just a memory address pointing to where you've got your integers. An array is nothing more than a pointer, hence the lack of length information.


Yeah, I figured that out. Thanks for the explanation though. We are currently learning pointers, but I dont think ths homework requires the use of them.
 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
How can I get it to loop through an array until it reaches the value of -1? Can I do
while(i!=-1)? I think a for statement afterwards but what do I put in the middle? for(j=0,?,j++)?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
> Why isnt this working?

It looks like it should, provided there is a -1 in an array element and your array size is at least [ max_values +1]

(e.g. for 10 valid numbers you need [ 11 ] to hold them plus the -1)

Run with your debugger or add a print statement in the loop to see a[ i ]
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Ditto the above, although it looks weird. I've never seen the condition involve the array being iterated, but I guess it can't hurt. I'd normally leave that part to the inside of the loop and do a "break;". I'd also recommend changing your loop to the code below.

Note that it's critical that the "i < arraySize" goes before the "a[ i ] != -1". The other way around, you'd be checking one element off the end of the loop. But conditional statements are "short-circuited" so that once the check on i fails, it stops evaluating.

Edit: dang it, fell prey to the old [ i ] makes things italic :confused:
 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
Originally posted by: kamper
Ditto the above, although it looks weird. I've never seen the condition involve the array being iterated, but I guess it can't hurt. I'd normally leave that part to the inside of the loop and do a "break;". I'd also recommend changing your loop to the code below.

Note that it's critical that the "i < arraySize" goes before the "a[ i ] != -1". The other way around, you'd be checking one element off the end of the loop. But conditional statements are "short-circuited" so that once the check on i fails, it stops evaluating.

Edit: dang it, fell prey to the old [ i ] makes things italic :confused:



I get what you are saying but the array is user-defined. The user can enter up to 25 elements, but to stop the inputs they enter a -1. So, it has to loop until the -1 in order to know how many elements there are. I'll mess around with what you gave me.
 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
I think there is some stupid error somewhere. I keep getting the same values of 49 and 74. I have looked through the whole thing over and over and cant seem to find anything that would cause this.

I just added a printf statement to that loop and see that for the first set is not stopping until 49 and the second is not stopping until 74. I dont know why this is. Probably some stupid mistake somewhere but I dont know what it is.

My question has to do with the function at the bottom called setCardinality.
 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
I think I put to much of my code in the previous post, too much for you guys to have to scroll through.

Whats wrong here is that it is not recognizing when there is a -1 value in the array. This array is input by the user, it can be any amount of elements up to 25. -1 is the stop or terminating value. That is why I need to loop through and stop when a -1 value is read. But for some reason it keeps going. Here is just the lines where i think the trouble is.

 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
Just a quick look at your code, but when you're scanning the values in and the user enters a -1, you break before you insert that value into the array. ( The -1 is never inserted when you reach 25, either). Likewise, if you want to store 25 values, you should make your arrays of length 26, and *always* fill the last value with -1.

 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
Wait a sec! I added a printf for a in the loop. It is just showing random numbers. Is it being passed by reference or something? If i recall, individual array elements are passed by value.
 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
All my previous problems have been resolved. My new question is:

I have two functions which I will attach on here. I have 2 arrays in int main. Now I need to pass both arrays individually to these two functions. Everything is fine with the first one. On the second one, I am calling the first function within this function. My question is, what do I put in the spot, marked "/*here*/"? The matrices are a and b. I need to pass them individually so I dont know what to put there, or what else can I do?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
you should put the function prototype for setCardinality at the top of your file or in an .h header file, rather than inside of printSet

> /*here*/

just "a" since that is the variable and you want to pass te full array.

finally, re-calculating the cardinality for each iteration of the loop is not very elegant. try something like

int card = setCardinality( a ) ;

above your loop.
 

saahmed

Golden Member
Oct 5, 2005
1,388
1
0
1000th POST!

Originally posted by: DaveSimmons
you should put the function prototype for setCardinality at the top of your file or in an .h header file, rather than inside of printSet

> /*here*/

just "a" since that is the variable and you want to pass te full array.

finally, re-calculating the cardinality for each iteration of the loop is not very elegant. try something like

int card = setCardinality( a ) ;

above your loop.

EDIT: NEVERMIND GOT IT TO WORK.
I do have the function prototype at the top of the file. Int main has 2 arrays, 'a' and 'b' that are input by the user. They both get passed to setCardinality first. Then, I want to pass each array individully to printSet, and within that function I want to pass that same array to setCardinality. So I want only the array that I am passing to printSet, to pass to setCardinality within the function.