Home > Back-end >  generate a number sequence, and then sort the sequence
generate a number sequence, and then sort the sequence

Time:12-04

I am supposed to write a program that processes a sequence of 100 positive numbers. The program should have 3 different functions, which could be chosen from a menu.

  1. Generate a number sequence with a random generator and print the numbers on the screen. The numbers generated should be in the range 0 ≤ n ≤ 900

  2. I have to sort the sequence with bubble sort and then the numbers must be printed and I cannot use built-in sorting functions, such as qsort.

  3. Quit program. The 1 and 2 must be in own functions. Lasly every time the program prints out the number, it should be printed as a table with ten rows and ten columns. Choice two can not be made unless choice one has been chosen.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 100
#define N 10
#define INVALID 0
#define VALID 1

void randomNum() {
    //random number gen
    int num[SIZE] = {0};
    int i = 0;
    
    srand(time(NULL));
    
    
for(i = 0; i < SIZE; i  ) {
      
       num[i] =  rand() % 901;
    }
        for(i = 0; i < SIZE; i  ) {
        printf("M", num[i]);
        if(i % 10 == 9)
            printf("\n");
    }  
}

void sort() {
    //sort of the generated number sequence
    int num[SIZE] = {0};
    int i = 0;
    int j = 0;
    int temp = 0;
    
     for (int i = 0 ; i < SIZE; i  )
      {
            for (int j = 0; j < SIZE - 1; j  )
            {
              if (num[j] > num[j 1]) 
                {
                temp = num[j];
                num[j]   = num[j 1];
                num[j 1] = temp;
                  }
            }
        }
            
         for(i = 0; i < SIZE; i  ) {
        printf("M", num[i]);
        if(i % 10 == 9)
            printf("\n");
         }   
}


int main() {
    
    randomNum();
    printf("\n");
    
    sort();
    
    
    
    return 0;    
}

I have figured out the solutions for how to generate a sequene and sort it and it works correct when all the code are in main() , however, when I put in in own functions above the main() it does not work. I'm stuck and don't know how to move forward. When i run the program it generates a random sequence but then the sorting function just prints out 100 zeros.

CodePudding user response:

I fixed up your code, to try to do mostly the same thing in a very straight forward fashion.

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

#define SIZE 100

void fill_random_array( int array[const], size_t sz )
{
    srand(time(NULL));  
    for(int i = 0; i < sz;   i)
    {
        array[i] =  rand() % 901;
        printf("=%s", array[i], (i 1)? " ": "\n" );
    }  
}

void sort( int array[const], size_t sz )
{
    for (int i = 0 ; i < SIZE; i  )
    {
        for (int j = 0; j < SIZE - 1; j  )
        {
            if (array[j] > array[j 1]) 
            {
                int temp   = array[j];
                array[j]   = array[j 1];
                array[j 1] = temp;
            }
        }
    }
}


int main()
{
    int data[SIZE];
    fill_random_array( data, SIZE );
    printf("\n\nResult:\n");
    sort( data, SIZE );
    
    for(int i = 0; i < SIZE;   i)
    {
        printf("=%s", data[i], (i 1)? " ": "\n" );
    }  

    return 0;    
}

Example Output:

Success #stdin #stdout 0s 5536KB
757 872 260 877 827 747 839 279 468 311 361 584 382 364 528  24
848  32 307 479 594  59 172  54 811 530 550 451 618 893  39 474
261 597 450 187 740 686 467 307 393 224 288 775 588 816 195 832
848 502 707 838 859 879 892 769 806 839 616 523 831 656  97 191
352 844 676 488 629 539 796 121 763 183 896 747 395 190  74 639
 89 781 873  47 759 865 212  60 199 828 584 129 880  77 618 628
 20 690 216 650 

Result:
 20  24  32  39  47  54  59  60  74  77  89  97 121 129 172 183
187 190 191 195 199 212 216 224 260 261 279 288 307 307 311 352
361 364 382 393 395 450 451 467 468 474 479 488 502 523 528 530
539 550 584 584 588 594 597 616 618 618 628 629 639 650 656 676
686 690 707 740 747 747 757 759 763 769 775 781 796 806 811 816
827 828 831 832 838 839 839 844 848 848 859 865 872 873 877 879
880 892 893 896 

CodePudding user response:

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

#define SIZE 100
#define N 10

void fill_random_array( int array[SIZE],  size_t size )
{
int i= 0;
srand(time(NULL));  
for(i = 0; i < SIZE;   i)
{
    array[i] =  rand() % 901;    
} 
for(i = 0; i < SIZE; i  ) {
    printf("M", array[i]);
    if(i % 10 == 9)
        printf("\n");
}
}

void sort( int array[SIZE], size_t size )
{
int i = 0;
int j = 0;
for (i = 0 ; i < SIZE; i  )
{
    for (j = 0; j < SIZE - 1; j  )
    {
        if (array[j] > array[j 1]) 
        {
            int temp   = array[j];
            array[j]   = array[j 1];
            array[j 1] = temp;
        }
    }
}
for(i = 0; i < SIZE; i  ) {
    printf("M", array[i]);
    if(i % 10 == 9)
        printf("\n");
     }   
}

void calculations(int array[SIZE], size_t size) {
//max och min

int i = 0;
int max = 0;
int min = 900;
int sum = 0;
double average = 0;
double median = 0;
for(i = 0; i < SIZE; i  ){

    if(array[i] > max) {
        max = array[i];   
    }
    if(array[i] < min) {
        min = array[i];
    }    
}  
//average
for(i = 0; i < SIZE; i  ) {
    sum  = array[i];
}
average = sum / SIZE;
//median
median = (array[49]   array[50])/2;
printf("Max: %d  Min: %d  Average: %f  Median: %f\n", max, min, average, median);          
}

void binsearch(int array[SIZE], size_t size) {
//binary search

int i = 0;
int pos = 0; //position
int start = 0; //start position i vector
int end = 99; //end position i vector
int number = 0;
int flag;
int row = 0; //row
int col = 0; //column
printf("Enter number: \n");
scanf("%d", &number);
flag = 0;
while(start <= end) {
    
    pos = (start   end)/2;
    if(array[pos] == number){
        row = (pos / N)   1;
        col = (pos % N)   1;
        printf("The number %d was found in position %d and row %d and column     %d\n", number, pos, row, col);
         
        flag = 1;
        break;
    }
    else if(array[pos] < number)
        start = pos   1;
    else if(array[pos] > number)
        end = pos - 1;
    
    break;
}
 if(flag == 0)
     printf("Number not found\n");     
}    

int main()
{
int data[SIZE];
fill_random_array( data, SIZE );
printf("\n");
sort( data, SIZE );
printf("\n");
calculations( data, SIZE);
printf("\n");
binsearch(data, SIZE);

return 0;    
}

This is what my code looks like now. The program works okay now I think but when I run it, it generates a random number sequence, and sort it and then print out max min, average value and median. But when I enter a number for the binarysearch it says: number not found even though the number is in the sorted sequence, how come it does that? And how do I fix it?

  •  Tags:  
  • c
  • Related