LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-10-2004, 07:16 PM   #1
']['HeBroken
Member
 
Registered: Oct 2004
Posts: 35

Rep: Reputation: 15
'c' 10int sort


hey all i did a search for this but didnt come out good, so i need a little bit of help here. Ok i need to create a program that will take in 10 int's and show the highest , lowest , average and sum. How the hell do i do this?. im so in the dark with this. any help will be appreciated thank you in advance...
 
Old 11-10-2004, 08:21 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I'd use an array probably:
Code:
int sort[10];
 
Old 11-10-2004, 08:37 PM   #3
']['HeBroken
Member
 
Registered: Oct 2004
Posts: 35

Original Poster
Rep: Reputation: 15
i tried that. erm what i need to do is take the ten ints, swap them and sort them according to size... im so friken stumped on how to do this..
 
Old 11-10-2004, 09:18 PM   #4
']['HeBroken
Member
 
Registered: Oct 2004
Posts: 35

Original Poster
Rep: Reputation: 15
ok ill make it easier for meself and u guys.. ill show the code..

Code:
#include <stdio.h>
#include <stdlib.h>



#define MAX 10

int iNums(int *);
int iLow(int *);
int iLar(int *);
int iSum(int *);
float fAver(float *);


int main()
{
	int iNums[MAX];
	int *ptr_iNums;
	int iCount = 0;
	int *ptr_iSum;
	int *ptr_iLow;
	int *ptr_iLar;
	float *ptr_fAver;



	for ( iCount = 0; iCount <MAX; iCount++)
	{
		printf("\n GIVE ME NUMBER %d", iCount + 1);
		scanf("%d", &iNums[iCount]);
	}

}

now what im trying to do is take all the numbers it takes in and sort them and show them to the screen. i did a google search but all i got was crap results and very confusing ones. if any help thank you muche....
 
Old 11-10-2004, 10:29 PM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You can do a very inefficient sort pretty easily. It won't matter that it's inefficient with 10 measly numbers, but you'd want to use a better algorithm with a larger set. The code would be something like this:

Code:
outer loop
  inner loop
    if sort[outer loop] is less than sort[inner loop]
      set temporary int to sort[outer loop], set sort[outer loop] to sort[inner loop], set sort[inner loop] to temporary int
 
Old 11-10-2004, 10:44 PM   #6
']['HeBroken
Member
 
Registered: Oct 2004
Posts: 35

Original Poster
Rep: Reputation: 15
alrighty this is what i got so far.. this is probably so f-ed up its not funny but here it goes..


Code:
#include <stdio.h>
#include <stdlib.h>

/******************************************************************
			
			11/11/04
			10 int's sorted Large, low, aver and sum
********************************************************************/

#define MAX 10

int iNums(int *);
int iLow(int *);
int iLar(int *);
int iSum(int *);
float fAver(float *);

void main()
{
	int iNums[MAX];
	int *ptr_iNums;
	int iCount = 0;
	



	for ( iCount = 0; iCount <MAX; iCount++)
	{
		printf("\n GIVE ME NUMBER %d", iCount + 1);
		scanf("%d", &iNums[iCount]);
	}
	*ptr_iNums = *iNums;



}
// Starting the swaping 
void swap(int *ptr_iNums)
{
	int temp;
	temp = *ptr_iNums;
	*ptr_iNums = *ptr_iNums;
	*ptr_iNums = temp;
}


		void sort *iNums();
		{
		int iswap = iCount;
		while(swap)
	}
		iSwap = 0;
			for (iCount = 0; iCount <MAX; iCount++)
		{
		if(iNums[iCount] <= iNums[iCount])
		{
		swap(*ptr_iNums, *ptr_iNums)
		}
		iswap = 1;
		}
now does that look anything close to what im trying to do?....


this is the errors that i get when i do it...

Code:
c:\P4V2\P4V2.c(53): error C2059: syntax error : ')'
c:\P4V2\P4V2.c(47): error C2143: syntax error : missing '{' before '*'
c:\P4V2\P4V2.c(47): error C2040: 'iNums' : 'int *()' differs in levels of indirection from 'int (int *)'
c:\P4V2\P4V2.c(48): error C2449: found '{' at file scope (missing function header?)
c:\P4V2\P4V2.c(51): error C2059: syntax error : '}'
c:\P4V2\P4V2.c(53): error C2059: syntax error : 'for'
c:\P4V2\P4V2.c(53): error C2143: syntax error : missing '{' before '<'
c:\P4V2\P4V2.c(53): error C2059: syntax error : '<'
c:\P4V2\P4V2.c(53): error C2143: syntax error : missing '{' before '++'
c:\P4V2\P4V2.c(53): error C2059: syntax error : '++'
thank you for takin your time to help me. im such a newb when it comes to this.......
 
Old 11-10-2004, 10:48 PM   #7
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Well, those syntax errors should be easy enough to fix. One problem you're having is that you're passing 2 values to swap(), but swap() is only accepting one. Your indentation is atrocious. Cleaning that up so you can easily follow blocks of code usually helps in the debugging process immensely.
 
Old 11-10-2004, 11:36 PM   #8
']['HeBroken
Member
 
Registered: Oct 2004
Posts: 35

Original Poster
Rep: Reputation: 15
all im getting now is

Code:
c:\P4V2\P4V2.c(59): error C2059: syntax error : '}'
c:\P4V2\P4V2.c(46): error C2449: found '{' at file scope (missing function header?)
and they are pointing to these right here....


Code:
void sort (int *ptr_iNums);
		{ <------ THIS ONE
		int iswap = iCount;
		while(swap)
		
		iswap = 1;
		
			for ( iCount = 0; iCount <MAX; iCount++ )
		if(iNums[iCount] <= iNums[iCount])
		{
		swap( *ptr_iNums, *ptr_iNums );
		
		iswap = 2;
		}
		} <--------- & this one
They are in the right places right?
 
Old 11-11-2004, 12:10 AM   #9
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
1.Why sort the file? just parse it and compare the values with the lowest / largest
2.DO NOT use global variables unless it's really necessery.

Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 10

float my_func(int *Nums,int *Sum,int *Low,int *Lar)
{
	int count;
	*Sum =*Low = *Lar = Nums[0];                                   /* initialize Low/Lar/Sum*/  
	for (count = 1; count < MAX;count++)
	{
		if (*Low > Nums[count]) *Low = Nums[count];  /*if Nums[count] is lower than current Low*/
		if (*Lar < Nums[count]) *Lar = Nums[count];     /*if Nums[count] is larger than current Lar*/
		*Sum += Nums[count];                                          /*add Nums[count] to sum */
	}
	return *Sum/10.0;                               /*10.0 for casting the result to float, returning the Average*/
}

int main()
{
	int Nums[MAX];
	int Count = 0;
	int Sum,Low,Lar;
	float Aver;
	printf("\n\t* c 10int *\n\n");
	for ( Count = 0; Count < MAX; Count++)
	{
		printf("GIVE ME NUMBER %d :", Count+1);
		if(!scanf("%d", &Nums[Count]))
		{
		fprintf(stderr,"Wrong Input\n");
		exit(0);
		}	
	}
	Aver = my_func(Nums,&Sum,&Low,&Lar);
	printf("\n\t*RESULTS*\n\n");
	printf("Lowest:\t\t%d\nLargest:\t%d\nSum:\t\t%d\nAver:\t\t%.1f\n",Low,Lar,Sum,Aver);
	return 0;
}
 
Old 11-11-2004, 12:17 AM   #10
']['HeBroken
Member
 
Registered: Oct 2004
Posts: 35

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by perfect_circle
1.Why sort the file? just parse it and compare the values with the lowest / largest
2.DO NOT use global variables unless it's really necessery.

Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 10

float my_func(int *Nums,int *Sum,int *Low,int *Lar)
{
	int count;
	*Sum =*Low = *Lar = Nums[0];                                   /* initialize Low/Lar/Sum*/  
	for (count = 1; count < MAX;count++)
	{
		if (*Low > Nums[count]) *Low = Nums[count];  /*if Nums[count] is lower than current Low*/
		if (*Lar < Nums[count]) *Lar = Nums[count];     /*if Nums[count] is larger than current Lar*/
		*Sum += Nums[count];                                          /*add Nums[count] to sum */
	}
	return *Sum/10.0;                               /*10.0 for casting the result to float, returning the Average*/
}

int main()
{
	int Nums[MAX];
	int Count = 0;
	int Sum,Low,Lar;
	float Aver;
	printf("\n\t* c 10int *\n\n");
	for ( Count = 0; Count < MAX; Count++)
	{
		printf("GIVE ME NUMBER %d :", Count+1);
		if(!scanf("%d", &Nums[Count]))
		{
		fprintf(stderr,"Wrong Input\n");
		exit(0);
		}	
	}
	Aver = my_func(Nums,&Sum,&Low,&Lar);
	printf("\n\t*RESULTS*\n\n");
	printf("Lowest:\t\t%d\nLargest:\t%d\nSum:\t\t%d\nAver:\t\t%.1f\n",Low,Lar,Sum,Aver);
	return 0;
}

Whooo hooo you are awsome.... that was even simpler than the first.. thank you muchies.......
 
  


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
C++ Sort() bendeco13 Programming 2 11-02-2005 11:26 PM
how to sort the output of ls bahadur Programming 18 03-28-2005 07:08 PM
Hello (sort of...) Gethyn LinuxQuestions.org Member Intro 1 10-15-2004 12:10 PM
sort pantera Programming 5 05-26-2004 07:36 PM
Bubble sort? Becks Programming 4 09-12-2003 01:28 PM

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

All times are GMT -5. The time now is 12:51 PM.

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