Thread: Functions and Pointers

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    60

    Functions and Pointers

    Hey guys I have just begone learning functions and pointers in class and we were given a question to do for test preparation and I am totally lost.

    The question wants me to make a function that asks the user to enter as many integers as possible and if they enter 0 it exits.

    conditions:
    it ignores all negative numbers
    it sums all odd and even numbers separately
    it finds the largest integer

    The prototype must be int count(int *sumOdd, int *sumEven, int *largest);

    b)write another function that prints the report using the prototype:
    void printresult(int tot, int sum1, int sum2, int max);

    c)call the two functions in the main

    sample run:

    number:2,number:9,number:12,number:8,number:25,num ber:44, number:0
    summary
    You entered 6 numbers(excluding 0)
    the sum of even is 54
    the sum of odd is 34
    the largest is 44

    This is what I have tried so far but I am very lost, Plz help

    Code:
    #include<stdio.h>
    
    int count(int *sumOdd, int *SumEven, int *largerst);
    void printresult(int tot,int sum1,int sum2,int max);
    
    
    
    
    int count(int *sumOdd, int *SumEven, int *largerst)
    {
    int num,check;
    	do{
    		printf("Enter as many integers as you like(0 to exit)");
    		scanf("%d",&num);
    		
    		if(num<0)
    		{
    		
    		}
    			else if(num%2==0)
    			{
    			*SumEven=num+num;
    			}
    				else
    				{
    				*sumOdd=num+num;
    				}
    				
    		check=check+1;
    		
    	while(num!=0);
    	
    	
    	return check;
    		
    
    
    }
    
    
    void printresult(int tot,int sum1,int sum2,int max)
    {
    printf("You entered %d numbers",tot);
    printf("the sum of all the odd numbers is %d",sum1);
    printf("the sum of all the even numbers is %d",sum2); 
    printf("the largest number is %d",max);
    }
    
    
    
    int main()
    {
    count();
    printresult();
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Have you tried to compile your code? Did you get any error messages? If so what are they (Please show complete error messages)? What is your program doing/not doing?

    Jim

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    it tells me everything is undeclared in the void printresult function and it tells me that there are to few arguments in the main function and I know that becuase I am not sure how to do that. And I am also missing the sort in the int count function.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Look at your prototype for count:
    Code:
    int count(int *sumOdd, int *SumEven, int *largerst);
    and how you are calling it in main()
    Code:
    count();
    You must call the functions with the same number and types of parameters.
    In main()

    Code:
    int sumOdd,  SumEven,  largerstcount;
    count(&sumOdd, &SumEven, &largerstcount) // pass the address of 3 int variables.
    You must also pass the variables into your other function.

    Jim

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    I just have a couple more questions. By me doing that in the main function it will get all the values that are pointers, but how will it get the return value? Second what do I do with the void printresult in the main function?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You might want to take a look at this link C Tutorial. It has sections on pointers, and defining and using functions.

    Jim

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    Ok I got the program to run now. The only problem I am having is a logical one. For the first part I am not sure how I can find out what the largest number is. Second right now the program is priting out crazy answers.

    Code:
    #include<stdio.h>
    
    int count(int *sumOdd, int *SumEven, int *largerst);
    void printresult(int tot, int sum1, int sum2, int max);
    
    
    int main()
    {
    
    int sumo,sume,large,a1,a2,a3,a4;
    count(&sumo,&sume,&large);
    printresult(a1,sumo,sume,large);
    }
    
    
    
    
    int count(int *sumOdd, int *SumEven, int *largerst)
    {
    int num,check,ignore;
    	do{
    		printf("Enter as many integers as you like(0 to exit)");
    		scanf("%d",&num);
    		
    		if(num<0)
    		{
    		ignore=num-num;
    		}
    			else if(num%2==0)
    			{
    			*SumEven=num+num;
    			}
    				else
    				{
    				*sumOdd=num+num;
    				}
    				
    	check=check+1;
    		
    	}while(num!=0);
    	
    	
     return check;
    		
    }
    
    
    
    
    
    void printresult(int tot, int sum1, int sum2, int max)
    {
    printf("You entered %d numbers\n",tot);
    printf("The sum of of even is %d\n",sum2);
    printf("The sum of odd is %d\n",sum1);
    printf("the largest number is %d\n",max);
    }
    I entered in numbers,5 2 and 0.

    My result:
    4199232 numbers entered
    10 is the odd sum
    0 is the even sum
    -1 is the largest number
    Last edited by Thegame16; 10-17-2010 at 10:53 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You get crazy results because your code is crazy. Everytime you scan a number, you assign 2 x that number to ignored, even or odd.
    check is never initialized. Thus it is not strange you get junk.
    ignore is not initialized either and its results are never used.
    Your indentation also leaves a lot to be desired.

    As for the max value problem, first describe how you would do it if you were given such a problem in reality.
    Last edited by Elysia; 10-17-2010 at 11:04 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    Quote Originally Posted by Elysia View Post
    You get crazy results because your code is crazy. Everytime you scan a number, you assign 2 x that number to ignored, even or odd.
    so what do I have to do to get the continuous numbers they enter to be added as even or odd?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You obviously need to add the number to the previous sum, no?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    Quote Originally Posted by Elysia View Post
    You obviously need to add the number to the previous sum, no?
    like this?

    Code:
    	if(num<0)
    		{
    		ignore=ignore-num;
    		}
    			else if(num%2==0)
    			{
    			*SumEven=*SumEven+num;
    			}
    				else
    				{
    				*sumOdd=*sumOdd+num;
    				}
    when I do this I still have crazy answers, do you know what else is wrong?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, that's correct. Though I don't know what you're trying to do with ignore.
    Also see my edits in my reply above.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    Quote Originally Posted by Elysia View Post
    Yes, that's correct. Though I don't know what you're trying to do with ignore.
    Also see my edits in my reply above.
    so I initialized check and ignore=0.
    For the ignore part I don't want any of those numbers to be added as even or odd.
    Now for finding the max value: I know if it was two numbers it would look like this

    Code:
    if(num1>num2)
    {
    large=num1;
    }
         else
         {
          large=num2;
         }
    but I don't know how to do it with an infinite amount of numbers possibly being entered/

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you want to ignore something, then just ignore it. You don't need it, so why add it to a variable in the first place?

    Now, imagine yourself being given N numbers written on one piece of paper each, in reality. Describe how you would find the paper containing the largest of those values.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    compare each number individually and then you would know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of functions
    By frktons in forum C Programming
    Replies: 29
    Last Post: 06-30-2010, 09:51 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM