Go Back   nV News Forums > Software Forums > Software Development

Newegg Daily Deals

Reply
 
Thread Tools
Old 09-17-10, 02:51 AM   #1
mr_mooo
Registered User
 
Join Date: Sep 2010
Posts: 2
Default 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
mr_mooo is offline   Reply With Quote
Old 09-17-10, 01:18 PM   #2
FlakMagnet
Registered User
 
FlakMagnet's Avatar
 
Join Date: Mar 2005
Location: Farnborough, UK
Posts: 335
Default 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.
__________________
ASUS P6T Deluxe V2 | i7 920 @ 3.6GHz
POV GTX480 | 6GB Patriot DDR3 @1443
X-Fi Fatal1ty Pro | 120GB OCZ Vertex SSD
2xSamsung 7200 750GB RAID 0| Akasa Eclipse 62
FlakMagnet is offline   Reply With Quote
Old 09-17-10, 02:17 PM   #3
lduguay
Un-Ripped
 
Join Date: Oct 2004
Location: Montreal, Canaduh!
Posts: 1,968
Default 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.
__________________

Evga X58|I7 965 3.8GHz|Ultra 120 Extreme|Evga GTX 480 SC|2 x Intel X25-M 80GB RAID 0|Intel 520 - 240 GB|WD Black Caviar 1TB|6GB Corsair XMS3 1600C8 |Corsair 1000W PSU|Antec Twelve-Hundred|DELL U3011|Logitech G500|Logitech G19
lduguay is offline   Reply With Quote
Old 09-17-10, 11:27 PM   #4
mr_mooo
Registered User
 
Join Date: Sep 2010
Posts: 2
Default 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
mr_mooo is offline   Reply With Quote
Old 03-24-12, 03:33 AM   #5
smithdavid
*BANNED*
 
Join Date: Mar 2012
Posts: 2
Default 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.
smithdavid is offline   Reply With Quote
Old 03-24-12, 09:44 AM   #6
wnd
Nerd, Geek, Freak
 
wnd's Avatar
 
Join Date: Sep 2005
Location: Finland
Posts: 703
Default Re: C programming issue

Nice signature. ;-)

Quote:
Originally Posted by smithdavid View Post
"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...
__________________
web | cat

Christianity, noun: The belief that a cosmic Jewish Zombie who was his own father can make you live forever if you symbolically eat his flesh and telepathically tell him you accept him as your master, so he can remove an evil force from your soul that is present in humanity because a rib-woman was convinced by a talking snake to eat from a magical tree. [mad.frog]
wnd is offline   Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 07:37 PM.


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