• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

C problems

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
PHP:
int getStringReg(char s[]) { 
    int i; 
    gets(s); 
    if (s[0] == '\0') 
        return 0; 
    for (i = 0; s[i] != '\n'; i++) { 
        if (s[i] == ' ') 
            return -1; 
    } 
    return 1; 
}

Trying to write a small function that returns a string, but only accepting one single word. So i tried to return -1 to the calling function when the input is invalid (space was found). But -1 is never returned.
 
Last edited:
Level 15
Joined
Nov 30, 2007
Messages
1,202
Works for me.

Here is the related code:
PHP:
        do {
            printf("%-38s ", msgTeam1);
            status = getStringReg(strTeam1);
            if (!status)
                return 0;
        } while (status == -1);

Another problem:
PHP:
void bubbleSort(struct gameData match[], const int *games) {
    struct gameData tmp;
    int i, dateSum[*games+1];
    bool sort;
    for (i = 1; i <= *games; i++) {
        dateSum[i] = match[i].d.day + match[i].d.month*100 + match[i].d.year * 10000;
        printf("%d\n", dateSum[i]);
    }
    puts("HELLO?");
    do {
        sort = false;
        for (i = 1; i < *games; i++) {
            if (dateSum[i] > dateSum[i+1]) {
                tmp = match[i];
                match[i] = match[i+1];
                match[i+1] = tmp;
                sort = true;
            }
        }
    } while (sort);
}

I', trying to sort the dates from largest to smallests so why not use to least effective sorting algorithm out there? Anyway. It loops into infinity...
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
The sorting algorithm for bubble sorting works with two regular loops; usually ones goes from i=0 to i=count-1, the other one goes from count-1 to i, and swaps the values if the given condition is true. Your sorting algorithm does something completely different >_<

You prefer this?:
PHP:
 #include <stdio.h>

struct date {
    int sum;
};

void bubbleSort(struct date d[], int n) {
    struct date tmp;
    int i, j;
    for (i = 0; i <= n; i++) {
        for (j = 0; j < n; j++) {
            if (d[j].sum > d[j+1].sum) {
                tmp = d[j+1];
                d[j+1] = d[j];
                d[j] = tmp;
            }
        }
    }
}

int main() {
    struct date d[10];
    d[0].sum = 20001110;
    d[1].sum = 20091110;
    d[2].sum = 20001110;
    d[3].sum = 19901110;
    d[4].sum = 20001110;
    d[5].sum = 20141110;
    d[6].sum = 20001110;
    d[7].sum = 20001110;
    d[8].sum = 20331110;
    d[9].sum = 20001110;

    bubbleSort(d, 9);

    int i;
    for (i = 0; i < 10; i++)
        printf("%d\n", d[i].sum);

    exit(0);
}

I need help validating the input of a date. So far the user enters: yyyy-mm-dd. But I need help how to check if the date actually ever existed. Looks like this so far:

PHP:
int validateDateStr(char s[], struct date *d) {
    if (strlen(s) == 10) {
        int i;
        char year[5], month[3], day[3];
        for (i = 0; i < 4; i++) {
            if (isDigit(s[i]))
                year[i] = s[i];
            else
                return -1;
        }
        year[4] = '\0';
        for (i = 5; i < 7; i++) {
            if (isDigit(s[i]))
                month[i-5] = s[i];
            else
                return -1;
        }
        month[2] = '\0';
        for (i = 8; i < 10; i++) {
            if (isDigit(s[i]))
                day[i-8] = s[i];
            else
                return -1;
        }
        day[2] = '\0';
        struct date tmp;
        strcpy(tmp.str, s);
        tmp.year = atoi(year);
        tmp.month = atoi(month);
        tmp.day = atoi(day);
        tmp.sum = tmp.year * 10000 + tmp.month * 100 + tmp.day;
        *d = tmp;
        return 1;
    }
    return -1;
}
 
Last edited:
Level 15
Joined
Oct 18, 2008
Messages
1,588
no, that's not right :p
Code:
 #include <stdio.h>

struct date {
    int sum;
};

void bubbleSort(struct date d[], int n) {
    struct date tmp;
    int i, j;
    for (i = 0; i < n; i++) {
        for (j = i; j < n; j++) {
            if (d[i].sum > d[j].sum) {
                tmp = d[j];
                d[j] = d[i];
                d[i] = tmp;
            }
        }
    }
}

int main() {
    struct date d[10];
    d[0].sum = 20001110;
    d[1].sum = 20091110;
    d[2].sum = 20001110;
    d[3].sum = 19901110;
    d[4].sum = 20001110;
    d[5].sum = 20141110;
    d[6].sum = 20001110;
    d[7].sum = 20001110;
    d[8].sum = 20331110;
    d[9].sum = 20001110;

    bubbleSort(d, 9);

    int i;
    for (i = 0; i < 10; i++)
        printf("%d\n", d[i].sum);

    exit(0);
}

The logic behind is that you take the first element, and keep swapping it if you find a bigger (or smaller) one until you reach the end of the array. Then you move on to the second element - you don't check the first one again, since you already know that it's bigger (or smaller) than the second element since it's the biggest number. You go through the rest of the elements and do the same.
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
Or just call qsort to sort anything

How do you do it if you want to sort the following:

PHP:
 #include <stdio.h>
#include <stdlib.h>

int values[] = { 88, 56, 100, 2, 25 };

int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)b - *(int*)a );
}

int main()
{
   int n;

   printf("Before sorting the list is: \n");
   for( n = 0 ; n < 5; n++ ) {
      printf("%d ", values[n]);
   }

   qsort(values, 5, sizeof(int), cmpfunc);

   printf("\nAfter sorting the list is: \n");
   for( n = 0 ; n < 5; n++ ) {
      printf("%d ", values[n]);
   }

  return(0);
}

Replace Values with the following struct:
PHP:
struct date {
   int sum;
}
struct values {
     struct date d;
}

Where date d[].d.sum is what I want sorted in this case.
 
Status
Not open for further replies.
Top