LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-28-2002, 10:40 AM   #1
jetfreggel
Member
 
Registered: May 2002
Posts: 172

Rep: Reputation: 30
c: array of poiners(integer or double)


hi everyone

i'm learning c from a book

there ids an exercice; Write a progam that uses pointers of the type integer
(or double) from the user sort them and print them out

i am approaching this with an array of pointers

i am only interrested in how i could put the var that i collect with scanf and put
and pointing to the and print it out
i am trying for two days now

this the best solution i got now
#include <stdio.h>

#define MAX 10


int main(void)
{
int count = 0 , tmp, cnt;
int nbr1[MAX];
for(;;)
{
scanf("%d", &tmp);


if (count < MAX && tmp != 0 )
{
nbr1[count++] = tmp;
}
else
break;
}
for(cnt = 0; cnt < count; cnt++)
{
printf("%d\n", nbr1[cnt] );
}
return 0;
}

but i don't think that's what there asking?????

Last edited by jetfreggel; 12-28-2002 at 11:17 AM.
 
Old 12-28-2002, 12:04 PM   #2
jetfreggel
Member
 
Registered: May 2002
Posts: 172

Original Poster
Rep: Reputation: 30
sorry, sorry, sorry i think i posted to early because i think i got it
(on my own not surfing the net until i found something)
here i go
[code]
#include <stdio.h>

#define MAX 10
int get_numbers(int p[]);
void sort(int p[] , int n);
void print(int p[] , int n);
int nbr[MAX];
int main(void)
{
puts("Enter 10 integers( 0 to exit)");
int aant_nbr = 0;
aant_nbr = get_numbers(nbr);
sort(nbr, aant_nbr);
print(nbr, aant_nbr);
return 0;
}
int get_numbers(int p[])
{
int count = 0 , tmp;
for(;
{
scanf("%d", &tmp);
if (count < MAX && tmp != 0 )
{
nbr[count++] = tmp;
}
else
{
return count;
break;
}
}
}
void sort(int p[] , int n)
{
int a, b;
int tmp;
for ( a = 0 ;a < n ; a++)
{
for (b = 1 ; b < n-1; b++)
{
if ( nbr[b] > nbr[b+1])
{
tmp = nbr[b];
nbr[b] = nbr[b+1];
nbr[b+1] = tmp;
}
}
}
}


void print(int p[] , int n)
{
int cnt;
for(cnt = 0; cnt < n; cnt++)
{
printf("%d\n", p[cnt] );
}
}
[code/]

Last edited by jetfreggel; 12-30-2002 at 10:09 AM.
 
Old 12-29-2002, 01:55 AM   #3
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Is this what you wanted to do? If so I think that the syntax is cleaner:

Code:
#include <stdio.h>

void FillArray ( int a[], int size )
{
  int i;
  for ( i = 0; i < size; ++i)
    {
      printf("Enter integer #%d :", i + 1);
      scanf("%d", &a[i] );
      while (getchar() != '\n');
    }
}

void SortArray(int  a[], int size)
{
  int i;
  int ii;
  int temp;

  for ( i = 0; i < size; ++i)
    {
      for ( ii = i + 1; ii < size; ++ii)
	{
	  if( a[i] > a[ii])
	    {
	      temp = a[i];
	      a[i] = a[ii];
	      a[ii] = temp;
	    }
	}
    }
}

void DisplayArray( int  a[], int size)
{
  int i;
  for (i = 0; i < size; ++i)
    {
      printf("%d \n", a[i]);
    }
}

int main()
{
  int iArray[10];
  int size = sizeof(iArray) / sizeof (int );

  FillArray( iArray, size );
  SortArray( iArray, size );
  printf("Sorted List:\n");
  DisplayArray( iArray, size);
  return 0;
}
 
Old 12-29-2002, 08:39 AM   #4
jetfreggel
Member
 
Registered: May 2002
Posts: 172

Original Poster
Rep: Reputation: 30
thx again for you reply

and i will try to studie the syntax note and try to use them
(caude they make the program much clearer)
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
HEELLP ~~ Converting an integer minute into time, Rounding off a double ~~ HELLP Mistro116@yahoo.com Programming 5 10-04-2005 11:51 AM
double matrix array in c alaios Programming 3 09-15-2005 11:34 AM
Double the desktop, not double the fun! bizshop SUSE / openSUSE 3 08-26-2005 12:22 PM
how to convert long integer value to byte array appas Programming 11 11-23-2004 01:56 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration