ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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...
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....
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
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.
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;
}
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.......
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.