Deutsch   English   Français   Italiano  
<667da789$1$7076$882e4bbb@reader.netnews.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news-out.netnews.com!postmaster.netnews.com!us12.netnews.com!not-for-mail
X-Trace: DXC=CWNac=c75lN=LX4bSbU0nMHWonT5<]0TMQ;nb^V>PUfF`L[ofA1LK4AL]Xbg<imJ2D\M??[m]0\@D3mESbIcX<ROWjB\<Me8\AG@GM\IYO6WZD>Oa:X5b?f8H
X-Complaints-To: support@blocknews.net
Date: Thu, 27 Jun 2024 13:55:22 -0400
MIME-Version: 1.0
User-Agent: Betterbird (Windows)
From: DFS <nospam@dfs.com>
Subject: Re: Find "py.exe" & copy it to "Python" (flat, no extension).
Newsgroups: comp.os.linux.advocacy
References: <v24179$1c3qk$2@dont-email.me> <v246gl$1d8oq$3@dont-email.me>
 <lamt35Fcgq7U4@mid.individual.net> <v26rjl$21vl0$1@dont-email.me>
 <v2ccjg$39nup$2@dont-email.me> <69sj4j50b5jb8mnbc37b1aopn58vpj0a5q@4ax.com>
 <v33cu1$ddl1$10@dont-email.me> <2Nj5O.33580$9xU7.24227@fx17.iad>
 <v36bpp$10k3v$1@dont-email.me> <PKE5O.17601$8CY1.13682@fx37.iad>
 <v3j4vt$3j4v3$1@dont-email.me>
 <665d1d57$0$2363138$882e4bbb@reader.netnews.com>
 <v5dlar$1dttg$2@dont-email.me> <memk7j5epu65t50ajn5vcbamj5s5uku5ig@4ax.com>
 <v5dpgc$1eh23$6@dont-email.me> <c8xeO.13067$zMs3.416@fx48.iad>
 <v5eb77$1hofv$1@dont-email.me> <le0f32F1qhiU1@mid.individual.net>
 <v5gr69$23k0t$1@dont-email.me> <le2ubhFd873U1@mid.individual.net>
Content-Language: en-US
In-Reply-To: <le2ubhFd873U1@mid.individual.net>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 144
Message-ID: <667da789$1$7076$882e4bbb@reader.netnews.com>
NNTP-Posting-Host: 127.0.0.1
X-Trace: 1719510921 reader.netnews.com 7076 127.0.0.1:42959
Bytes: 4275

On 6/26/2024 12:36 PM, rbowman wrote:


> I have better things to do with my life
> than taking on an army of 'fixers'.  DFS is bad enough.


Somewhat new C challenge!

* determine the mode of a set of random, positive ints *


My code is below.  To make the challenge easier, it generates and sorts 
the random numbers for you.

$ ./prog N  (to run it)

Refer to my solution whenever you want, but preferably after you've 
written yours.

For extra credit, determine/print the mode(s) using only one pass thru 
the dataset. I didn't do that - I looped it twice.

Enjoy!












































====================================================================
//this program determines the mode of a set of integers
//this code is hereby donated to the public domain

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

//comparator function used with qsort
int compareint (const void * a, const void * b)
{
   if (*(int*)a > *(int*)b) return 1;
   else if (*(int*)a < *(int*)b) return -1;
   else return 0;
}

int main(int argc, char *argv[])
{
	//vars
	int N = atoi(argv[1]);
	int *nums  = malloc(N * sizeof(int));
	int i = 0;
	
	//------------------------------------------------------------------
	//generate random numbers
	//------------------------------------------------------------------
	int low = 1, high = N*3;
	srand(time(NULL));
	for(i=0;i<N;i++) {
		nums[i] = (low + rand() / (RAND_MAX / (high - low + 1) + 1));
	}
	
	printf("%d randoms between 1 and %d\n", N, 3*N);
	for(i=0;i<N;i++) { printf("%d ", nums[i]); }
	qsort(nums,N,sizeof(int),compareint);
	printf("\nSorted\n");
	for(i=0;i<N;i++) { printf("%d ", nums[i]); }
	
	//------------------------------------------------------------------
	//determine mode
	//------------------------------------------------------------------
	int maxoccur = 0, dmax = nums[N-1];
	int *modecnt = calloc(dmax + 1, sizeof(int));	
	
	//get count of each value in dataset
	for(i = 0; i < N; i++)   { 						
		modecnt[nums[i]]++;
		if(modecnt[nums[i]] > maxoccur) {
			maxoccur = modecnt[nums[i]];
		}	
	}				
	
	//print each value that occurs the most often
	if (maxoccur > 1) {	
		printf("\n\nmode: ");
		for(i = 0; i <= dmax; i++) {
			if(modecnt[i] == maxoccur) {
				printf("%d ",i);
			}
		}
		printf("(%d occurrences ea.)", maxoccur);
	}	
	
	//no repeating values
	if (maxoccur == 1) {	
		printf("\nmode: na (no repeating values)");
	}
	
	free(modecnt);
	free(nums);
	
	printf("\n");
	return 0;
}
====================================================================