Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 22nd, 2005, 5:59 AM   #1
paulmedic555
Newbie
 
paulmedic555's Avatar
 
Join Date: Jan 2005
Location: Greece
Posts: 18
Rep Power: 0 paulmedic555 is on a distinguished road
Send a message via MSN to paulmedic555
return a value from a function

Hi all i am new to programming and after i learnt Pascal i try to learn C.I have working to this program and from the pinakas function i want to return to main the first negative value of the array or if there isn't a negative value to return the first value of the array.Here is the code
#include<stdio.h>
#define n  5
int *pinakas(int *pin);
main()
{ 
     int *ptr,pin[n],i;

    //fill the array
    printf("Dwse ta stoixeia toy pinaka :\n");
    for(i=0;i<n;i++)
         scanf("%d",&pin[i]);
    ptr=pinakas(pin);
    printf("%d\n",*ptr);
}

int *pinakas(int *pin)
{
   int k;

   for (k=0;k<n;k++)
  {
       if(*(pin+k)<0)
             return pin+k;
       else
            return pin;   
  }
}
This code return me always the first value of the array and not the first negative value if there is.
Any help?
paulmedic555 is offline   Reply With Quote
Old Jan 22nd, 2005, 7:00 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
The problem is that your function always returns a value on the first iteration. If it's negative, it returns it, otherwise, it returns it anyway. You want:
int *pinakas (int *pin)
{
    int k;
    
    for (k = 0; k < n; k++)
    {
        if (*(pin + k) < 0)
            return (pin + k);
    }
    return pin;
}
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:53 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC