5 Replies - 719 Views - Last Post: 12 November 2008 - 08:59 PM Rate Topic: -----

#1 sweetlava  Icon User is offline

  • New D.I.C Head

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

One dimensional table with functions in C

Post icon  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>


	  // reads 10 numbers
void readNumbers (int numbers[10])
{	
	 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  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3122
  • View blog
  • Posts: 19,167
  • 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 readArray(int[]);
void printArray(int[]);
void printReverse(int[]);
void greaterThenTen(int[]);
void evenNums(int[]);

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

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

void printArray(int arr[])
{
	printf("Your array in sequential order:\n");
	for (int i = 0; i < 10; i++)
	{
		printf("%d", arr[i]);
		printf(" ");
	}
	return;
}

void printReverse(int arr[])
{
	printf("Your array in reverse order:\n");
	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;
}


Was This Post Helpful? 1
  • +
  • -

#3 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • 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>

void readNumbers(int *arry)
{
	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;
}


Was This Post Helpful? 0
  • +
  • -

#4 sweetlava  Icon User is offline

  • New D.I.C Head

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

Re: One dimensional table with functions in C

Posted 12 November 2008 - 08:46 PM

View Postbrds, 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>

void readNumbers(int *arry)
{
	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 ?
:blink:
I'm truely new at this!
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,857
  • 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

Was This Post Helpful? 1
  • +
  • -

#6 brds  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: One dimensional table with functions in C

Posted 12 November 2008 - 08:59 PM

View Postsweetlava, on 12 Nov, 2008 - 09:46 PM, said:

To brds
We haven't learned pointers yet.
How do I use global variables ?
:blink:
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 */

void readNumbers()
{
	int i;

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

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

int main(void)
{
	int i;

	readNumbers();

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

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1