just testing out sorting methods and I've come across selection sort. I've understood the logic behind the selection sort but I don't get the desired result i wish to see from this program. It doesn't seem to sort at all. Could someone tell me where i went wrong.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int StudentCreation(int StudentRecordArray[10]){
for(int i = 0; i < 10; i++){
StudentRecordArray[i] = rand() % 100; //limiting the marks range from 0 - 100
}
}
int SelectionSort(int SelectionSortarray[]) {
int n = 0;
int tmp = 0;
for(int j = 0; j < 10-1; j++){
int TempMinimum = j;
for(int i = j+1; i < n; i++)
if(SelectionSortarray[i] < SelectionSortarray[TempMinimum])
TempMinimum = i;
if(TempMinimum != j){
tmp = SelectionSortarray[j];
SelectionSortarray[j] = SelectionSortarray[TempMinimum];
SelectionSortarray[TempMinimum] = tmp;
}
}
for (int f = 0; f < 10; f++){
printf("Student %d - %d\n", f+1, SelectionSortarray[f]);
}
}
int main() {
int StudentRecord[10];
int MenuChoice;
srand(time(NULL)); //random number seed generator
StudentCreation(StudentRecord);
printf("the unsorted list:\n");
for (int f = 0; f < 10; f++){
printf("Student %d - %d\n", f+1, StudentRecord[f]);
}
printf("\nthe sorted list:\n\n");
SelectionSort(StudentRecord);
return 0;
}
Is it something wrong with where i have swapped?
There is a typo in the sorting function.
Instead of
int n = 0;
there should be
int n = 10;
Related
I want to create an array of 100 size, which its elements are unique random integers from 1 to 999999. My code doesn't give any error message or the output that I want. What is wrong with this?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 100
#define EMPTY -1
int main() {
srand(time(NULL));
int list[999999], A[N], i;
for (i = 0; i < N; i++)
A[i] = EMPTY;
for (i = 0; i < 999999; i++) {
list[i] = i + 1;
}
for (i = 0; i < 999999; i++) {
int j = rand() % 999999;
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
for (i = 0; i < N; i++) {
A[i] = list[i];
}
for (i = 0; i < N; i++) {
printf("%i\n", A[i]);
}
}
The initial loop is useless, the method is very inefficient, but the output should meet the goal...
Yet there might be an issue with the list array: it is very large and defining it as a local variable with automatic storage cause a stack overflow, depending on your target system. Try defining is as:
static int list[999999];
Here is an alternative for N small compared to 1000000:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 100
int main() {
int A[N], i, j;
srand(time(NULL));
for (i = 0; i < N;) {
int n = 1 + rand() % 999999;
for (j = 0; j < i; j++) {
if (A[j] == n)
break;
}
if (i == j)
A[i++] = n;
}
for (i = 0; i < N; i++) {
printf("%i\n", A[i]);
}
return 0;
}
#include <stdio.h>
#include <math.h>
#define MAX_SIZE 10000
int main(void)
{
int a[MAX_SIZE];
int N;
int L; /* the current size of the list */
/* read in the upper limit. Keep reading until
a valid number between 3 and the maximum that
can be handled by the array is entered */
double b[10000];
int j, i;
L = 0;
printf("Enter the upper limit:\n");
do {
scanf("%d", &N);
} while (N<3 || N>MAX_SIZE+2);
int prime;
for (j = 1; j < N; j++)
{
prime = 1;
for (i = 2; i < j; i++)
{
if (j % i == 0)
{
prime = 0;
break;
}
}
if (prime)
{
a[i] = j;
L++;
}
}
/* write out the result - DO NOT CHANGE THIS */
for(i=0;i<L;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
Program needs to take an integer, calculate primes below that integer, print that list of primes.
I think my problem is related to the loops.
The program is calculating the primes but listing 0 if the number previously there isnt prime eg a[4] is now printing as 0
Any help is appreciated.
thanks.
Is this what you were trying to implement?
#include <stdio.h>
#define MAX_SIZE 100
int main(void)
{
int primes[MAX_SIZE];
int primes_found = 0;
int limit = 0;
while (limit < 3)
{
printf("Enter the upper limit:\n");
scanf("%d", &limit);
}
for (int candidate = 2; candidate <= limit && primes_found < MAX_SIZE; candidate++)
{
int divisor = 2;
int is_prime = 1;
while(is_prime && divisor < candidate)
is_prime = candidate % divisor++ != 0;
if (is_prime)
primes[primes_found++] = candidate;
}
for (int i = 0 ; i < primes_found ; i++)
printf("%d ", primes[i]);
printf("\n");
return 0;
}
I am making this code for a random dice roller and I want to get a random number until I get three 6's on the dice. For some reason this produces the same number for all runs, but on my for loop it works. Can I get some help getting this to work?
The pesky While loop in question:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int Ran(int max) {
return (rand() % max)+ 1;
}
int main(){
int max = 6;
int nsuc = 0, ncount = 0;
int matt = 40;
srand((unsigned) time(0));
for(int i = 1; i <= 20; i++){
while(!(nsuc >= 3)){
int nr = Ran(max);
if(nr < matt){
ncount++;
if(nr == 6){
nsuc++;
}
}
}
printf("Number of rolls until 3 6's = %d\n", ncount);
}
}
Cleaned up the for loop so it's easier to read now:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void Shuffle() {
srand((unsigned) time(NULL));
}
int Ran(int max) {
return (rand() % max)+ 1;
}
void binomial(int run, int max, int btrials, int *array, int scount){
for(int j = 1; j<=run; j++){
for (int i=1;i<=btrials;i++){
int r = Ran(max);
if(r == 6){
scount++;
}
printf("Trial %i, roll = %d\n",i , r);
}
array[j-1] = scount;
scount = 0;
printf("\nEnd of Run %d\n\n",j);
}
}
int main(){
int Runs;
printf("How many runs do you want to do? > ");
scanf("%d", &Runs);
printf("\n");
int bdist[20];
int distcount = 0;
int max = 6;
int btrials = 20;
int suc[Runs+1];
int scount = 0;
Shuffle();
binomial(Runs, max, btrials, suc, scount);
printf("All counts of succesful 6 rolls.\n");
/*
for(int z = 1; z<=Runs; z++){
printf("Run %d has %d 6's\n", z, suc[z-1]);
}
*/
for(int k = 0; k<=btrials; k++){
for(int j = 1; j<=Runs; j++){
if(suc[j-1] == k){
distcount++;
}
}
bdist[k] = distcount;
distcount = 0;
}
for(int t = 0; t<=btrials; t++){
printf("Number of runs with %d 6s = %d\n", t, bdist[t]);
}
return 0;
}
You never reset nsuc or ncount in the first version with the while loop. After the first time thru, they still have their values from the previous loop so you get the same output.
Not an answer strictly related to the question, but something I noticed.
If I recall, generating ranged random numbers using the (rand % max)+ 1 idiom is not advised.
One.
Two
I have task about printing board where you should input dimension and program will create a board with 2 numbers inside each cell: 1'st number is random from 1-3 and second one should be zero. I can make it only with zero but when i tried to make a random number everything went wrong way..
Maybe anyone knowns what it wrong with it?
Function calls uploading map:
int randfunc(int i, int n);
int uploadmap(int m,int n){
int a[m][n];
int i,j,k;
// time_t t;
//srand((unsigned)time(&t));
//int randnum = rand() % 3 + 1;
for(i = 0; i < m;i++){
printf("+---");
}
printf("+\n");
memset(a,0,sizeof(a));
for(i = 0;i < m;i++){
for(j = 0; j < n;j++){
printf("|%d %d",randfunc(i,n),a[i][j]);
}
printf("|\n");
for(k = 0;k < m;k++){
printf("+---");
}
printf("+\n");
}
return 0;
}
function which calls random numbers from 1 to 3:
int randfunc(int i, int n) {
time_t t;
srand((unsigned) time(&t));
for( i = 0 ; i < n ; i++ ) {
printf("%d\n", rand() % 3 + 1);
}
return 0;
}
Main function :
int main(int argc, const char * argv[]) {
int m,n;
printf("Enter dimension: \n");
scanf("%d %d",&m, &n);
printf("Map has been uploaded %d\n",uploadmap(m,n));
return 0;
}
The following simple code could work:
#include <stdio.h>
#include <stdlib.h>
void uploadmap(int m,int n) {
for (int i = 0;i < m; ++i) {
for(int j = 0; j < n; ++j)
printf("|%d 0", (rand() % 3)+1);
printf("|\n");
}
}
int main() {
int m, n;
printf("Enter dimension: \n");
scanf("%d %d",&m, &n);
uploadmap(m,n);
return 0;
}
I have got some more problems with the code. This program ask the user to specify the nr of throws then it throws 3 dices and add these 3 dices to sum.
Then another function sorts the sum form the smallest to the largest with a bubble sorting algorithm.
the first two functions seems to work but the program does not print out the result of the 3rd sorting function.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 100
//This function ask the user for the amout of throws
int numberofthrows() {
int throws
printf("Type in the number of throws");
scanf("%d", &throws);
return throws;
}
//This function makes the random throws of 3 dices with regard to the number of throws
int filler(int thrownr, int dice1[MAX], int dice2[MAX], int dice3[MAX], int sum[MAX]) {
int i, nr;
srand(time(NULL));
for(i = 0; i <= thrownr; i++) {
nr = rand()%6;
dice1[i] = nr + 1;
nr = rand()%6;
dice2[i] = nr + 1;
nr = rand()%6;
dice3[i] = nr + 1;
sum[i] = dice1[i] + dice2[i] + dice3[i];
}
int j;
for(j = 0; j <= thrownr; j++) {
printf("%d ", dice1[j]);
printf("%d ", dice2[j]);
printf("%d ", dice3[j]);
printf("%d\n", sum[j]);
}
}
//This function sorts the result in form the sum array
int sorter(int thrownr, int sum[MAX], int sortsum[MAX]) {
int tmp, i, j, k, m;
for(i = 0; i <= thrownr; i++) {
sortsum[i] = sum[i];
}
for(m = 0; m <= 10; m++) {
for(j = 0; j <= thrownr; i++) {
if (sortsum[j] > sortsum[j+1]) {
tmp = sortsum[j];
sortsum[j] = sortsum[j+1];
sortsum[j+1] = tmp;
}
}
}
for(k = 0; k <= thrownr; k++) {
printf("%d\n", sortsum[k]);
printf("%d\n", sum[k]);
}
}
int main(void) {
srand(time(NULL));
int dice1[MAX];
int dice2[MAX];
int dice3[MAX];
int sum[MAX];
int sortsum[MAX];
int numberofthrows2;
numberofthrows2 = numberofthrows();
filler(numberofthrows2, dice1, dice2, dice3, sum);
sorter(numberofthrows2, sum, sortsum);
return 0;
}
The code for sorting is a bit wrong. Change
for(m = 0; m <= 10; m++)
To
for(m = 0; m <= thrownr-1; m++)
And
for(j = 0; j <= thrownr; i++)
To
for(j = 0; j < thrownr-m-1; i++)
To fix it. Also, call srand once at the start of main. Don't call it more than once in a program or you might get the same "random" numbers everytime you run your program.