Results 1 to 3 of 3

Thread: FUNCTION & ARRAY ?!?!?!?!?

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Posts
    3

    Angry

    It's a function "my" that takes a integer array that arguments.
    Function shall return a pointers to the smallest numbers.
    Problems is:
    Why receives I 11 numbers instead receives 10 and that answer latest numbers of them ?????

    Examples code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    int *my (int *ta, int an)
    {
    int *temp;
    temp = talen;
    for (int i=0;i<an;i++)
    {
    if (ta < temp)
    {
    temp = ta;
    }
    ta++;
    }
    return (temp);
    }
    main()
    {
    int ta[10];
    int *sv;
    srand ((unsigned) time(NULL) );
    ta[0]=rand();
    for (int i=0; i<10; i++)
    cout<<rand()<<endl;
    cout << ta [0]<< " ";
    cout << endl;
    sv = my(ta, sizeof(ta) /sizeof(ta[0]));
    cout<< " Smallest numbers is: " << *sv << endl << endl;
    cout <<"\nPress any key to continue\n";
    getch();
    return(0);
    }



  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Erm, I'm having trouble reading that so I'm going to try and repost the code using the code tags, and I'll tidy it up a bit too...

    Code:
    #include <iostream.h> 
    #include <stdlib.h> 
    #include <time.h> 
    
    int *my (int *ta, int an) 
    {  int *temp; 
       temp = talen; 
       for (int i=0;i<an;i++) 
       {  if (ta < temp) 
             temp = ta; 
          ta++; 
       } 
       return (temp); 
    } 
    
    int main() 
    {  int ta[10]; 
       int *sv; 
       srand ((unsigned) time(NULL) ); 
       ta[0]=rand(); 
       for (int i=0; i<10; i++) 
          cout<<rand()<<endl; 
       cout << ta [0]<< " "; 
       cout << endl; 
       sv = my(ta, sizeof(ta) /sizeof(ta[0])); 
       cout<< " Smallest numbers is: " << *sv << endl << endl; 
       cout <<"\nPress any key to continue\n"; 
       getch(); 
       return(0); 
    }
    Right, now I'm gonna have a look at it, I'm not sure if that's how you meant the code to be (I think there may be some {} brackets missing, and I took out any that looked unecessary.
    Harry.

    "From one thing, know ten thousand things."

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Okay, I think this code should work:

    Code:
    #include <iostream.h> 
    #include <stdlib.h> 
    #include <time.h> 
    
    int* my(int* ta, int an) 
    {	int* temp; 
    	temp = ta; 
    	for(int j=0;j<an;j++) 
    	{	if(*ta < *temp) 
    			temp = ta; 
    		ta++; 
    	} 
    	return(temp); 
    } 
    
    int main() 
    {	int ta[10]; 
    	int *sv; 
    	srand((unsigned)time(NULL));  
    	
    	//Initialise ta[] array with random integers
    	for(int j=0; j<10; j++)
    	{	ta[j]=rand();
    		cout << "ta[" << j << "] = " << ta[j] << endl; 
    	}
    
    	sv = my(ta, sizeof(ta)/sizeof(ta[0])); 
    	cout<< " Smallest numbers is: " << *sv << endl << endl; 
    		 
    	return(0); 
    }
    It works for me, I'm using MS Visual C++ 6. I'm not sure what you're using; you have used C-style casts so I suppose you're using C. I've tried to stick to what you used, with corrections.

    (I've edited this message to use j instead of i as a counter, because the i in square brackets is italic tags)

    [Edited by HarryW on 06-20-2000 at 02:07 PM]
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width