Re: My problem in C

Though there is a min, max, and minmax function in the C++ stdlib, your problem is in C, not C++. This is where a nice sorting algorithm comes into play. I will take the code from this page on RosettaCode and implement it into your program to show you how you would do it (this is with Heapsort, though other algorithms will work too). This will complicate your code, but it will also (always) give you the largest and smallest values in your list.

#include <stdio.h>

long long max (long long* a, long long n, long long i, long long j, long long k) {
long long m = i;
if (j < n && a[j] > a[m]) {
m = j;
}
if (k < n && a[k] > a[m]) {
m = k;
}
return m;
}
 
void downheap (long long* a, long long n, long long i) {
while (1) {
long long j = max(a, n, i, 2 * i + 1, 2 * i + 2);
if (j == i) {
break;
}
long long t = a[i];
a[i] = a[j];
a[j] = t;
i = j;
}
}
 
void heapsort (long long* a, long long n) {
for (long long i = (n - 2) / 2; i >= 0; i--) {
downheap(a, n, i);
}
for (long long i = 0; i < n; i++) {
int t = a[n - i - 1];
a[n - i - 1] = a[0];
a[0] = t;
downheap(a, n - i - 1, 0);
}
}

int main() {
int n;
long long maximum = 0;
long long minimum = 0;
printf("Enter a number between 1 to 300:");
scanf("%d",&n);
long long* list = (long long*)malloc(sizeof(long long)*n);
for (long long i = 0; i < n; i++) {
printf("Enter the number %d",i+1);
scanf("%d",&list[ i ]);
}
heapsort(list, n);
// Element 0 contains smallest number, element n-1 contains largest.
// ...
}

This could be greatly simplified to be a much smaller LOC count, but this also works (and isn't overblown, since you now, with that list, can get any number in a sorted way). Don't forget to free() the memory that I allocated (or you can rewrite it to suit your needs). I would encourage you to go try implementing this on your own though; I may have provided a sample, but you won't learn anything by copy-pasting it. This code is also versatile; you can easily replace the sorting algorithm I chose with something different if you so choose.

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Dark Eagle via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : stewie via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Dark Eagle via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Dark Eagle via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : pauliyobo via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Dark Eagle via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : pauliyobo via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : pauliyobo via Audiogames-reflector

Reply via email to