# One dimensional table with functions in C

Page 1 of 1

## 5 Replies - 1186 Views - Last Post: 12 November 2008 - 08:59 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=71663&amp;s=2ce5557f1c17c68609b1ef8596b58766&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sweetlava

Reputation: 1
• Posts: 10
• Joined: 09-October 08

# One dimensional table with functions in C

Posted 12 November 2008 - 06:55 PM

Hi,
I'm writing a program with functions which lets the user input 10 numbers and build a table of these 10 numbers.
It's supposed to display the 10 numbers in the order they were entered.
Then it is supposed to list these numbers in the reverse order they were entered.
Another list is supposed to display the numbers which are superior to 10.
Another is supposed to display the numbers divisible by 2.

I'm having a hard time writing the functions as well as the main program. This is my first program using functions.
Any help would be great.
Here's what I have so far,

```#include<stdio.h>
#include<ctype.h>

{
int i;
for( i=0; i<10; i++){

printf("\nEnter 10 numbers followed by a blank or enter:\n");
scanf( "%d", &numbers[i]);
printf(" %d", numbers[i]);

}
// inverses the list of numbers
void numInverse() {
int i, j, inverse;

for ( i=0; i<10; i++){
for( j=10; j>-1; j--)
inverse[i]=inverse[j];

}
// reads and validates numbers higher than 10
void plusTen (int plusten) {
int i;
if ( plusten>10) {
return plusten;
}
// reads and validates numbers divisible by 2
void Paire () {
int nb;
if ( nb%2==0){
printf("%d" , &nb);

return nb;
}
// system Pause
void Pause () {

printf(" Press a key to continue");
getch ();
} //endPause

// main program
int main ()
{
int number; plusTen, nb;

Pause ();
} // end main

```

Is This A Good Question/Topic? 0

## Replies To: One dimensional table with functions in C

### #2 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,233
• Joined: 14-September 07

## Re: One dimensional table with functions in C

Posted 12 November 2008 - 08:00 PM

Here's some code for thought:

```#include<stdio.h>
#include<ctype.h>

//function prototypes
void printArray(int[]);
void printReverse(int[]);
void greaterThenTen(int[]);
void evenNums(int[]);

int main()
{
int arr[10];
printf("\n");
printArray(arr);
printf("\n");
printReverse(arr);
printf("\n");
greaterThenTen(arr);
printf("\n");
evenNums(arr);
return 0;
}

{
for (int i = 0; i < 10; i++)
{
printf("Enter a number: ");
scanf("%d", &arr[i]);
}
return;
}

void printArray(int arr[])
{
for (int i = 0; i < 10; i++)
{
printf("%d", arr[i]);
printf(" ");
}
return;
}

void printReverse(int arr[])
{
for (int i = 9; i >= 0; i--)
{
printf("%d", arr[i]);
printf(" ");
}
return;
}

void greaterThenTen(int arr[])
{
printf("Numbers in your array greater then 10:");
for (int i = 0; i < 10; i++)
{
if (arr[i] > 10)
{
printf("%d", arr[i]);
printf(" ");
}
}
return;
}

void evenNums(int arr[])
{
printf("Numbers in your array divisible by 2:");
for (int i = 0; i < 10; i++)
{
if ((arr[i] % 2) == 0)
{
printf("%d", arr[i]);
printf(" ");
}
}
return;
}

```

### #3 brds

Reputation: 76
• Posts: 515
• Joined: 22-October 08

## Re: One dimensional table with functions in C

Posted 12 November 2008 - 08:13 PM

You will need to use pointers (or global variables ) if you want the numbers entered to be available outside of readNUmbers. All functions that need to change data within numbers, will also need a pointer passed to them.

```#include <stdio.h>

{
int i;

printf("Enter 10 integers :\n");

for(i = 0;i < 10; i++)
scanf("%d", arry++);
}

int main(void)
{
int i, numbers[10];

readNumbers(numbers); /* passing numbers is the same as &numbers[0] */

for(i = 0;i < 10; i++)
printf("%d\n", numbers[i]);

return 0;
}

```

### #4 sweetlava

Reputation: 1
• Posts: 10
• Joined: 09-October 08

## Re: One dimensional table with functions in C

Posted 12 November 2008 - 08:46 PM

brds, on 12 Nov, 2008 - 07:13 PM, said:

You will need to use pointers (or global variables ) if you want the numbers entered to be available outside of readNUmbers. All functions that need to change data within numbers, will also need a pointer passed to them.

```#include <stdio.h>

{
int i;

printf("Enter 10 integers :\n");

for(i = 0;i < 10; i++)
scanf("%d", arry++);
}

int main(void)
{
int i, numbers[10];

readNumbers(numbers); /* passing numbers is the same as &numbers[0] */

for(i = 0;i < 10; i++)
printf("%d\n", numbers[i]);

return 0;
}

```

To brds
We haven't learned pointers yet.
How do I use global variables ?

I'm truely new at this!

### #5 baavgai

• Dreaming Coder

Reputation: 7181
• Posts: 14,969
• Joined: 16-October 07

## Re: One dimensional table with functions in C

Posted 12 November 2008 - 08:50 PM

I agree with the need for pointers in the readNumbers function offered. However, I believe this particular one might be written in a way that's less confusing to someone unfamiliar with pointer quirks. i.e. keeping the array syntax.

```void readNumbers(int a[], int size) {
int i;

printf("Enter %d integers :\n", size);

for(i=0; i<size; i++) {
printf("%d: ", i + 1);
scanf("%d", &a[i]);
}
}

```

Edit: Don't use globals.

This post has been edited by baavgai: 12 November 2008 - 08:51 PM

### #6 brds

Reputation: 76
• Posts: 515
• Joined: 22-October 08

## Re: One dimensional table with functions in C

Posted 12 November 2008 - 08:59 PM

sweetlava, on 12 Nov, 2008 - 09:46 PM, said:

To brds
We haven't learned pointers yet.
How do I use global variables ?

I'm truely new at this!

Only because you asked, but remember globals are easily abused, generaly unneeded, and bad form too boot.

```#include <stdio.h>

int numbers[10]; /* Declare outside of any function */

{
int i;

printf("Enter 10 integers :\n");

for(i = 0;i < 10; i++)
scanf("%d", &numbers[i]);
}

int main(void)
{
int i;

for(i = 0;i < 10; i++)
printf("%d\n", numbers[i]);

return 0;
}

```