Skip to main content
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Post Reopened by Jamal
deleted 83 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I'm new to programming, but want to build up a good habit of code review so that I can develop best practice. It's been couple of weeks I'm coding in C and my knowledge, so far, is data types, conditions, arrayarrays, loop and, very minimal, pointerpointers.

I'm creating multiple search and sorting algorithms - not only to understand them better, but also solidifying my understanding of concepts. Any feedback on them will be highly appreciated.

Here's the most recent version:

I'm new to programming, but want to build up a good habit of code review so that I can develop best practice. It's been couple of weeks I'm coding in C and my knowledge, so far, is data types, conditions, array, loop and, very minimal, pointer.

I'm creating multiple search and sorting algorithms - not only to understand them better, but also solidifying my understanding of concepts. Any feedback on them will be highly appreciated.

Here's the most recent version:

I'm new to programming, but want to build up a good habit of code review so that I can develop best practice. It's been couple of weeks I'm coding in C and my knowledge, so far, is data types, conditions, arrays, loop and, very minimal, pointers.

I'm creating multiple search and sorting algorithms - not only to understand them better, but also solidifying my understanding of concepts.

added 1918 characters in body
Source Link

I'm creating multiple search and sorting algorithmssearch and sorting algorithms - not only to understand them better, but also solidifying my understanding of concepts. Any feedback on them will be highly appreciated.

Thanks.Here's the most recent version:

#include <stdio.h> 
#include <cs50.h>

void swap(int *x, int *);
int midval(int x, int y);

int main(void)
{
    // taking count for characters
    printf("How many characters are you entering? ");
    int count = get_int();
    
    // creating the array
    int array[count];
    
    // taking string as an input and converting them to integers
    for (int i=0; i<count; i++)
    {
        printf("Please enter the character number %i: ", (i+1));
        array[i] = atoi(get_string());
    }
    
    //sorting - bubble sort
    int j=count-1;
    do
    {
        for(int i=0; i<j; i++)
        {
            if (array[i] > array[i+1])
            {
                swap(&array[i], &array[i+1]);
            }
        }
        j--;
    }
    while (j!=0);
    
    //binary search
    printf("Please enter the character you want to search: ");
    int search = atoi(get_string());
    int i=0;
    
    do
    {
        if (search == array[midval(i, count-1)])
        {
            printf("The character exists in the list.\n");
            return 0;
        }
        else if (search > array[midval(i, count-1)])
        {
            i = midval(i, count-1) + 1;
        }
        else if (search < array[midval(i,count-1)])
        {
            count = midval(i, count-1);
        }
    }
    while (count != i);
    
    printf("The character does not exist in the list.\n");

    
}

void swap(int *x, int *y)
{
    int temp[1];
    temp[0] = *x;
    *x = *y;
    *y = temp[0];
}

int midval(int x, int y)
{
    int z = (x+y)/2;
    return z;
}

I'm creating multiple search and sorting algorithms - not only to understand them better, but also solidifying my understanding of concepts. Any feedback on them will be highly appreciated.

Thanks.

I'm creating multiple search and sorting algorithms - not only to understand them better, but also solidifying my understanding of concepts. Any feedback on them will be highly appreciated.

Here's the most recent version:

#include <stdio.h> 
#include <cs50.h>

void swap(int *x, int *);
int midval(int x, int y);

int main(void)
{
    // taking count for characters
    printf("How many characters are you entering? ");
    int count = get_int();
    
    // creating the array
    int array[count];
    
    // taking string as an input and converting them to integers
    for (int i=0; i<count; i++)
    {
        printf("Please enter the character number %i: ", (i+1));
        array[i] = atoi(get_string());
    }
    
    //sorting - bubble sort
    int j=count-1;
    do
    {
        for(int i=0; i<j; i++)
        {
            if (array[i] > array[i+1])
            {
                swap(&array[i], &array[i+1]);
            }
        }
        j--;
    }
    while (j!=0);
    
    //binary search
    printf("Please enter the character you want to search: ");
    int search = atoi(get_string());
    int i=0;
    
    do
    {
        if (search == array[midval(i, count-1)])
        {
            printf("The character exists in the list.\n");
            return 0;
        }
        else if (search > array[midval(i, count-1)])
        {
            i = midval(i, count-1) + 1;
        }
        else if (search < array[midval(i,count-1)])
        {
            count = midval(i, count-1);
        }
    }
    while (count != i);
    
    printf("The character does not exist in the list.\n");

    
}

void swap(int *x, int *y)
{
    int temp[1];
    temp[0] = *x;
    *x = *y;
    *y = temp[0];
}

int midval(int x, int y)
{
    int z = (x+y)/2;
    return z;
}
Post Closed as "Not suitable for this site" by Jamal
Source Link
Loading