6 Replies - 737 Views - Last Post: 21 October 2011 - 07:32 PM Rate Topic: -----

#1 Rover2cool   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 12-February 11

Help with Functions and arrays in C.

Posted 21 October 2011 - 06:03 PM

Hello, I having trouble with a part of my new project. Or getting started on it.

I need to create a function in c that gets 20 numbers -1 to stop and store it in a array.

So the teacher is asking:

Use a separate function for each of these actions:

1. Get the integers from user.

So the way I would write it normally is:

 
   printf("Enter integers. (Negitive -1 to stop):\n");
   for (i=0; i < 20; i++)
   {
   
    
    scanf("%d", &nums[i]);
    count = count +1;
    if(nums[i] == -1 )
    
    break;



Now I need to write that in a function...

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Functions and arrays in C.

#2 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Help with Functions and arrays in C.

Posted 21 October 2011 - 06:26 PM

So...what is your question?
Was This Post Helpful? 0
  • +
  • -

#3 Rover2cool   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 12-February 11

Re: Help with Functions and arrays in C.

Posted 21 October 2011 - 06:37 PM

View PostOler1s, on 21 October 2011 - 06:26 PM, said:

So...what is your question?



Soo.. this is what I got.. I thought you can only pass things to functions.. And you cant pass a array out of a function.. So how do I Create a function to get integers from a user..

#include <stdio.h>

//Prototypes

int getNums (int nums[]);


int main()
{
int nums[20];
getNums(nums);

return 0;

}

int getNums (int nums[],) //function 1
{
    int i;
    printf("Enter integers. (Negitive -1 to stop):\n");
    for (i=0; i<20; ++i)
    {
    scanf("%i" , &nums[i]);
    
    return nums[i];

}


Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Help with Functions and arrays in C.

Posted 21 October 2011 - 06:41 PM

I wrote the tutorials in my signature for a reason. Feel free to read them.
Was This Post Helpful? 1
  • +
  • -

#5 obviousninja   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 68
  • Joined: 17-February 10

Re: Help with Functions and arrays in C.

Posted 21 October 2011 - 07:10 PM

oh fun, can i give you the solution?
Was This Post Helpful? 0
  • +
  • -

#6 Rover2cool   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 12-February 11

Re: Help with Functions and arrays in C.

Posted 21 October 2011 - 07:13 PM

View Postobviousninja, on 21 October 2011 - 07:10 PM, said:

oh fun, can i give you the solution?


yeah sure im lost.
Was This Post Helpful? 0
  • +
  • -

#7 obviousninja   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 68
  • Joined: 17-February 10

Re: Help with Functions and arrays in C.

Posted 21 October 2011 - 07:32 PM

static void intReader(int *numArray, int input, int indexNow);
int main(){
  int *num, index, input, indexPrint;
  num = malloc(sizeof(int)*20);
  printf("please enter your input: (-1 to quit) \n");
  for(index=0; index<20; index++){
    scanf("%d", &input);
    if(input == -1){
      break;
    }
     
    intReader(num, input, index);

  }

  for(indexPrint=0; indexPrint<20; indexPrint++){
    printf("%d =>\t \n", num[indexPrint]);
  }

  return 0;
}
static void intReader(int *numArray, int input, int indexNow){
 
  numArray[indexNow] = input;
}


is this what you want??? you said "Use a separate function for each of these actions:" so i did.
or if you don't like dynamic array allocation, you can simply replace *num with num[20], careful though since i didn't initialize the array, the likelihood of statically allocated num[20] will generate random number for the uninitialized value. so you might want to initialize that.

This post has been edited by obviousninja: 21 October 2011 - 07:37 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1