Thread: Having problems with the rest of the code

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    20

    Having problems with the rest of the code

    The return values I keep getting are 4250916, can't figure out why.

    Code:
    #include <stdio.h>
    
    float myarray[10] = {1.66,.58,-2.35,0,1.9,0,-.34,0,7.56,-.3};
    int i=0,lesscount=0,morecount=0,zerocount=0;
    
    
    void main(){
    	
    	for(i=0 ; i<10 ; i++){
    	if (myarray[i] < 0){
    		lesscount++;
    		}
    	else if (myarray[i] == 0){
    		zerocount++;
    		}
    	else 
    		morecount++;
    			
    	}
    	printf("The number of values less than zero is %d\n\n",&lesscount);
    	printf("The number of values equal to zero is %d\n\n",&zerocount);
    	printf("The number of values more than zero is %d",&morecount);
    }

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Use lesscount not &lesscount

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    For printf(), you don't need to preceed variables with an ampersand. That's only for scanf.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Don't use void main

    > printf("The number of values less than zero is %d\n\n",&lesscount);
    Don't use & when you print things

    And you don't need all those variables to be global.

    Edit:
    Damn, beaten twice
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    Quote Originally Posted by Salem
    Don't use void main

    > printf("The number of values less than zero is %d\n\n",&lesscount);
    Don't use & when you print things

    And you don't need all those variables to be global.

    Edit:
    Damn, beaten twice

    Oh that's right thanks guys. How do I not make the variables global and how will that help me?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    int main ( ) {
        float myarray[10] = {1.66,.58,-2.35,0,1.9,0,-.34,0,7.56,-.3};
        int i=0,lesscount=0,morecount=0,zerocount=0;
    > How do I not make the variables global and how will that help me?
    Because once you get past more than one function, and certainly when you get past more than one source code file, masses of globals make your problems harder, not easier.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    15
    in fact when one uses the & symbol, not the value but the address of the memory location where the value was stored gets printed. since you want to print the value we must not use the &.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by alvarorahul
    in fact when one uses the & symbol, not the value but the address of the memory location where the value was stored gets printed. since you want to print the value we must not use the &.
    Unless you use * also.
    Code:
    int x = 5;
    printf("%d\n", *&x );


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 01:26 PM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  4. structs, array, code has problems, Im lost
    By rake in forum C++ Programming
    Replies: 4
    Last Post: 03-13-2002, 02:02 AM
  5. problems with output from code
    By simhap in forum C++ Programming
    Replies: 0
    Last Post: 10-08-2001, 12:43 PM