Thread: error C2664: cannot convert parameter 1 from 'int' to 'int []'

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    4

    error C2664: cannot convert parameter 1 from 'int' to 'int []'

    Hello all. I'm so new in C++ Programming that a simple problem seems extremely difficult!! The only thing that I have to do is to calculate the sum, the average, the max and the min of 10 numbers given by the user!! My code is:
    Code:
    #include <iostream>
    using namespace std;
    
    int count_sum(int numbers_par[]);
    void count_max(int numbers_par[]);
    void count_min(int numbers_par[]);
    
    int main()
    {
    	int numbers[10]={0,0,0,0,0,0,0,0,0,0};
    	int sum;
    	double average;
    	
    	cout<<"Enter 10 integers.\n";
    	for(int i=0; i<10; i++)
    		cin>>numbers[i];
    
    	sum = count_sum(numbers[10]);
    	
    	average = sum/10.0;
    	cout<<"average "<<average<<endl;
    	
    	count_max(numbers[10]);
    	
    	count_min(numbers[10]);
    	return 0;
    }
    
    int count_sum(int numbers_par[])
    {
    	int sum=0;
    	for(int i=0;i<10;i++)
    		sum = sum+numbers_par[i];
    	cout<<"sum "<<sum<<endl;
    	return sum;
    }
    
    void count_max(int numbers_par[])
    {
    	int max=0;
    	for(int i=0;i<10;i++)
    		if(numbers_par[i]>max)
    			max=numbers_par[i];
    	cout<<"max "<<max<<endl;
    }
    
    void count_min(int numbers_par[])
    {
    	int min=numbers_par[0];
    	for(int i=0;i<10;i++)
    		if(numbers_par[i]<min)
    			min=numbers_par[i];
    	cout<<"min "<<min<<endl;
    }
    But when I try to execute the latter some errors appear, which are:
    1. error C2664: 'count_sum' : cannot convert parameter 1 from 'int' to 'int []'
    2. error C2664: 'count_max' : cannot convert parameter 1 from 'int' to 'int []'
    3. error C2664: 'count_min' : cannot convert parameter 1 from 'int' to 'int []'

    Does anybody know how can I recover from all the above errors? Thanks a lot!

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    You define functions to expect pointers and not integer numbers.
    In line:
    sum = count_sum(numbers[10]);
    you're sending numbers[10] to function, which is 11-th element of array "number", which is also error because it's beyond of array limits.
    i would suggest to write function like this:
    Code:
    int count_sum(int numbers_par[], size_t len)
    {
    	int sum=0;
    	for(int i=0;i<len;i++)
    		sum = sum+numbers_par[i];
    	cout<<"sum "<<sum<<endl;
    	return sum;
    }
    Last edited by Micko; 12-15-2006 at 06:07 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    So what do you think I have to do? Can you tell me books for C++ programming. I think my school's notes are not enough! Thanks

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    But what about the call? Won't I face an other error which has to do with the parameters of the function? Which is the proper way to call those functions? Thans again.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    ok. I solve it! Thans

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    I'm glad you made it yourself. It's the best way to learn.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  7. #7
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    Quote Originally Posted by Micko
    I'm glad you made it yourself. It's the best way to learn.
    Exactly. There are so many people on this forum who just don't want to learn and want others to do their homework for them. That's disgusting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  2. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. 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