Thread: Multiplying numbers in an array

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    53

    Multiplying numbers in an array

    Hi,

    I'm trying to write a code, where numbers in an array, are multiplied by another array of numbers.
    In addition, once the multiplied array has reached its final number it has to loop again. Then continue multiplication till the array of of numbers have been multiplied.

    Here's an example of what I mean:
    5,5,7 -used for multiplication
    1,2,1,2,2,1,1 -array of numbers

    1x5, 2x5, 4x7,
    2x5, 2x5, 1x7,
    1x5


    Here's my code: -it doesn't compile, but I'm trying to find a way to fix it
    Code:
    int main()
    {
    	int sample[] = {1,2,4,5,7,9,1};
    
    	filterSamples(sample);
    	return 0;
    }
    
    int filterSamples(int& samples[])
    {
    	double countNumber = 0;
    	double filterNumbers[] = {2.5, 3.4, 5};
    	double *filterResults;
    
    	//filterResults = (double *) malloc(samples);
    
    	for(int i=0; i<samples.length(); i++)
    	{
    		filterResults = samples[i] * filterNumbers;
    		
    		countNumber++;
    	}
    	//finalNumbers = filterSamples;
    	cout << "filtered: " << countNumber << ". " << filterResults << "\n" << endl;
    	return 0;
    }
    Last edited by vopo; 11-08-2007 at 02:28 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are doing a few things "funny":
    Code:
    int main()
    {
    	int sample[] = {1,2,4,5,7,9,1};
    
    	filterSamples(sample);
    	return 0;
    }
    
    int filterSamples(int& samples[])
    {
    	double countNumber = 0;
    	double filterNumbers[] = {2.5, 3.4, 5};
    	double *filterResults;
    
    	//filterResults = (double *) malloc(samples);
    
    	for(int i=0; i<samples.length(); i++)
    	{
    		filterResults = samples[i] * filterNumbers;
    		
    		countNumber++;
    	}
    	//finalNumbers = filterSamples;
    	cout << "filtered: " << countNumber << ". " << filterResults << "\n" << endl;
    	return 0;
    }
    Filter results is a pointer. You are trying to assign a double to that pointer - this is invalid, and even if the compiler allowed it, wouldn't be what you wanted to do. Then you print your pointer, which is probably not what you wanted to do either.

    The input "samples" is an array of integer. You are trying to get the length() from an array of integer. This doesn't work - you need a vector or similar template class to do that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To Find Largest Consecutive Sum Of Numbers In 1D Array
    By chottachatri in forum C Programming
    Replies: 22
    Last Post: 07-10-2011, 01:43 PM
  2. multiplying a 2D array by a 1D array
    By youngvito in forum C Programming
    Replies: 14
    Last Post: 06-12-2009, 03:50 PM
  3. sorting an array of numbers in C
    By JVincent08 in forum C Programming
    Replies: 2
    Last Post: 03-14-2008, 01:42 PM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. getting numbers in an array
    By ct26torr in forum C Programming
    Replies: 6
    Last Post: 03-04-2003, 10:31 AM