GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Nov-2004, 17:38
justachessgame justachessgame is offline
New Member
 
Join Date: Oct 2004
Posts: 13
justachessgame is on a distinguished road

help with Sort arrays/Size


Can someone help me with writing this program please?

I have the first part done (part a). I can't get part (b) or (c).

How do I combine part b with part a...this all has to be one program.

My instructions are to:

Write a program that contain the following functions prototypes:

a) int Max( int a, int b );
Function Max takes two intergers as arguements and returns the value of
of the largest of the two intergers.

b) int Sum( int array[], int size );
Function Sum takes an array of intergers and an interger that indicates
the number of elements in the array. The function then returns the sum of
all the numbers in the array.

c) void Sort( int array [], int size ); //this one is really hard
Function Sort takes an array of intergers and an interger that indicates
the number of elements in the array. The function takes all the numbers in
the array and rearranges (i.e., sorts) them in ascending order.

This is what I have so far, as I said..I compiled part a successfully...but how do I do part b and c???

CPP / C++ / C Code:
#include <stdio.h>

int LinearSearch( int data[], int size, int num );
int BinarySearch( int data[], int size, int num );
void BubbleSort( int data[], int size );

int main()
{
    int a, b, max;
    
    printf ( "Enter a number:" );
    scanf ( "%d" , &a);
	printf ( "Enter a number:" );
    scanf ( "%d" , &b);
	
    if( a > b )
		max = a;
	else
		max = b;
		
	printf( "The greatest value is %d", max );
	
	return 0;
}

int LinearSearch( int data[], int size, int num )
{
	int t;
	
	for( t = 0; t < size; t++ ) {
		if( data[t] == num ) {
			return t;
		}	 	 
	}
	
	return -1;
}

int BinarySearch( int data[], int size, int num )
{
	// assume: data is sorted!
	int low, mid, high;
	
	low = 0;
	high = size;
	mid = (low + high) / 2;
	
	while( low <= high ) {
	
		if( data[mid] == num ) {
			return mid;
		}
		
		if( num < data[mid] ) {
			high = mid - 1;
		}
		else {
			low = mid + 1;	  
		}
		
		mid = (low + high) / 2;
	}
	
	return -1;	   
}


void BubbleSort( int data[], int size )
{
	int t;
	int temp;
	int swapDone;

	do {
		swapDone = 0;	 
		
		for( t = 0; t < size - 1; t++ ) {
			if( data[t] > data[t+1] ) {
				// have to swap the numbers
				temp = data[t];
				data[t] = data[t+1];
				data[t+1] = temp;
				
				swapDone = 1;
			}	 
		}
	} while( swapDone == 1 );
}
  #2  
Old 12-Nov-2004, 23:46
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by justachessgame
Can someone help me with writing this program please?

I have the first part done (part a). I can't get part (b) or (c).

How do I combine part b with part a...this all has to be one program.

My instructions are to:

Write a program that contain the following functions prototypes:

a) int Max( int a, int b );
Function Max takes two intergers as arguements and returns the value of
of the largest of the two intergers.

b) int Sum( int array[], int size );
Function Sum takes an array of intergers and an interger that indicates
the number of elements in the array. The function then returns the sum of
all the numbers in the array.

c) void Sort( int array [], int size ); //this one is really hard
Function Sort takes an array of intergers and an interger that indicates
the number of elements in the array. The function takes all the numbers in
the array and rearranges (i.e., sorts) them in ascending order.

This is what I have so far, as I said..I compiled part a successfully...but how do I do part b and c???
I'm not following you here. You say your instuctions are:
Quote:
Write a program that contain the following functions prototypes:

a) int Max( int a, int b );
...

This is what I have so far, as I said..I compiled part a successfully...
I see no prototype
CPP / C++ / C Code:
int Max( int a, int b );
at all, and no function as required. I think you had better start again...
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge sort on a linked list Temujin_12 C++ Forum 1 06-Mar-2008 20:33
Re: Piping and Redirection WaltP C Programming Language 1 11-Apr-2004 08:01
SORT / ORDER BY multi columns in MySQL misunderstood MySQL / PHP Forum 3 01-Oct-2003 09:01

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:18.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.