nV News Forums

 
 

nV News Forums (http://www.nvnews.net/vbulletin/index.php)
-   Software Development (http://www.nvnews.net/vbulletin/forumdisplay.php?f=53)
-   -   C programming issue (http://www.nvnews.net/vbulletin/showthread.php?t=155216)

mr_mooo 09-17-10 01:51 AM

C programming issue
 
Hey all,

I am fairly knew to the scene of C programming and I am having trouble with something I am trying to do.

I have an char array which contains a binary value of the output of 5 sensors. i.e. 00111

What I want to do is create a state machine from this array. I have tried using a switch statment..

switch (sensor_values)
{
case "00011":
// do somehing
break;
case "11000":
// do something else
break;
default:
break;
}

but I get the error "switch expression has illegal type"

Can i convert the array to something else so I can use my case statement or is there a better method?

Cheers,

Mr_Mooo

FlakMagnet 09-17-10 12:18 PM

Re: C programming issue
 
In C, you cannot perform a switch on character arrays, so you will need to convert your array into an integer value.

To do this, try something like the following. I haven't had a chance to see if the following compiled, but it should give you the idea.

int ConvertToInt(char val[])
{
int result = 0;

for (int pos = 0; pos < 5; pos++)
{
result = result << 1;

if (val[pos] == '1')
{
result |= 1;
}
}

return result;
}

You can then use this method to convert your array to a value and use it like this :

switch (ConvertToInt(sensor_values))
{
case 0x03: // 00011 in Hex.
// do somehing
break;
case 0x18: // 11000 in hex.
// do something else
break;
default:
break;
}

Hope that helps.

lduguay 09-17-10 01:17 PM

Re: C programming issue
 
FlakMagnet code works fine, you can also try this:

Code:

char        *pszSensorValues[] = {"00011","11000",0};
int        i=0;

while (pszSensorValues[i] && strcmp(sensor_values,pszSensorValues[i]) != 0)) i++;

switch (i) {

        case 0:
                  break;
        case 1:
                  ...
}

To make it cleaner, you can use an enum instead of index values.

mr_mooo 09-17-10 10:27 PM

Re: C programming issue
 
Excellent. Thanks for the quick responses!

I will give them a go and tell you how I went!

Cheers again,

Mr Mooo

smithdavid 03-24-12 02:33 AM

Re: C programming issue
 
I am trying to do a few practice problems from C by Dissection and can't figure out this problem at all.


The problem asks "write a program that reads n integers into an array, then prints on a separate line the value of each distinct element along with the number of times it occurs. the values should be printed in descending order."

it gives the example that the user inputs:

-7 -7 3 3 5 -7

and the program prints:

5 occurs 1 times
3 occurs 2 times
-7 occurs 3 times


it also asks that I use dynamic memory allocation and pointer arithmetic


if anyone can figure out this program that would be greatly appreciated.

wnd 03-24-12 08:44 AM

Re: C programming issue
 
Nice signature. ;-)

Quote:

Originally Posted by smithdavid (Post 2539532)
"write a program that reads n integers into an array, then prints on a separate line the value of each distinct element along with the number of times it occurs. the values should be printed in descending order."

and the program prints:

5 occurs 1 times
3 occurs 2 times
-7 occurs 3 times

it also asks that I use dynamic memory allocation and pointer arithmetic

Allocating memory dynamically is obvious, but what's the thing with pointer arithmetics here? Fine, I'll throw something in.

Using binary tree for storage and sorting:
Code:

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

#define POINTER_ARITHMETICS


struct tree;

struct tree {
        struct tree *left;
        struct tree *right;
        int val;
        int count;
};



static struct tree *
tree_new_node(int val)
{
        struct tree *node = malloc(sizeof(struct tree));
        assert(node);
        node->val = val;
        node->count = 1;
        node->left = node->right = NULL;
        return node;
}



static struct tree *
tree_insert(struct tree *node, int val)
{
        if (node) {
                if (val == node->val) {
                        node->count++;
                } else if (val < node->val) {
                        if (node->left) {
                                tree_insert(node->left, val);
                        } else {
                                node->left = tree_new_node(val);
                        }
                } else {
                        if (node->right) {
                                tree_insert(node->right, val);
                        } else {
                                node->right = tree_new_node(val);
                        }
                }
                return node;
        } else {
                return tree_new_node(val);
        }
}



static void
tree_print(struct tree *tree)
{
        if (tree->right) {
                tree_print(tree->right);
        }
        printf("%d occured %d time%s\n",
                        tree->val,
                        tree->count,
                        tree->count == 1 ? "" : "s");
        if (tree->left) {
                tree_print(tree->left);
        }
}



static void
tree_free(struct tree *tree)
{
        if (tree->left) {
                tree_free(tree->left);
        }
        if (tree->right) {
                tree_free(tree->right);
        }
        free(tree);
}



int
main(int argc, char **argv)
{
        struct tree *tree = NULL;
#ifdef POINTER_ARITHMETICS
        int *arr = NULL;
        int *arr_ptr;
        int i;
        int an = 0;

        while (1) {
                int val;
                int r;
                int *new_arr;

                r = scanf("%d", &val);
                if (r != 1 || r == EOF) {
                        break;
                }

                new_arr = realloc(arr, (an + 1) * sizeof(int));
                assert(new_arr);
                arr = new_arr;
                arr[an] = val;
                an++;
        }

        /* arr_ptr++ is your obligatory pointer arithmetics part ;-) */
        for (arr_ptr = arr, i = 0; i < an; arr_ptr++, i++) {
                int val = *arr_ptr;
                tree = tree_insert(tree, val);
        }
        free(arr);
#else
        while (1) {
                int val, r;

                r = scanf("%d", &val);
                if (r != 1 || r == EOF) {
                        break;
                }
                tree = tree_insert(tree, val);
        }
#endif

        tree_print(tree);
        tree_free(tree);

        return EXIT_SUCCESS;
}

Using flat arrays for storage and qsort for sorting:
Code:

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


struct instance {
        int        val;
        int        count;
};



static int
instance_cmp(const void *ap, const void *bp)
{
        const struct instance *a = ap;
        const struct instance *b = bp;

        return (a->val < b->val) ? 1 : ((a->val > b->val) ? -1 : 0);
}



int
main(int argc, char **argv)
{
        int *arr = NULL;
        int *arr_ptr;
        struct instance *instance = NULL;
        int an = 0;
        int n = 0;
        int i, j;

        while (1) {
                int val;
                int r;
                int *new_arr;

                r = scanf("%d", &val);
                if (r != 1 || r == EOF) {
                        break;
                }

                new_arr = realloc(arr, (an + 1) * sizeof(int));
                assert(new_arr);
                arr = new_arr;
                arr[an] = val;
                an++;
        }

        for (arr_ptr = arr, i = 0; i < an; arr_ptr++, i++) {
                int val = *arr_ptr;
                int seen = 0;
                int *new_instance;

                for (j = 0; j < n; j++) {
                        if (instance[j].val == val) {
                                instance[j].count++;
                                seen = 1;
                                break;
                        }
                }
                if (seen) {
                        continue;
                }

                new_instance = realloc(instance,
                                (n + 1) * sizeof(struct instance));
                assert(new_instance);

                instance = new_instance;
                instance[n].val = val;
                instance[n].count = 1;
                n++;
        }
        free(arr);

        qsort(instance, n, sizeof(struct instance), instance_cmp);

        for (i = 0; i < n; i++) {
                printf("%d occurs %d time%s\n", instance[i].val,
                                instance[i].count,
                                instance[i].count == 1 ? "" : "s");
        }

        free(instance);

        return EXIT_SUCCESS;
}


At first I was just looking at sample I/O and I wrote a version that inverts the tree (that is, allows ordering by count, not value). Then I reread the assignment...


All times are GMT -5. The time now is 05:59 PM.

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.