C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-14-2010, 11:39 AM   #1
Registered User
 
Join Date: Mar 2010
Posts: 1
Question help with array codes....plz,,,,,,beginner

Write a C-program to read 10 numbers from the keyboard and store the numbers in an array called numbers. After storing the numbers, do the following:

a) Find and print the sum and average of even and odd numbers in the array.
b) Find and print the largest and smallest element in the array. Also print the indexes of the largest element of the array.
shawndotting is offline   Reply With Quote
Old 03-14-2010, 11:43 AM   #2
Registered User
 
Join Date: Oct 2008
Posts: 943
Quote:
Originally Posted by shawndotting View Post
Write a

I stopped reading there.
EVOEx is offline   Reply With Quote
Old 03-14-2010, 03:35 PM   #3
Registered User
 
Join Date: Jan 2010
Posts: 238
Does everything except point a. You have to do something on your own right?
Code:
#include <linux/input.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>

int list_input_devices();
int* read_numbers(FILE* fd, int num);

int main()
{
	int numdev, i;
	int* numbers;
	const int numbers_to_read = 10;
	char temp[100];
	char output1[100], output2[100];
	int max, min;
	FILE* fd;

	numdev = list_input_devices();
	if(numdev == 0)
	{
		printf("Error. Couldn't find any usable input devices.\n");
		return -1;
	}
	do
	{
		printf("Enter the number of your keyboard device: ");
		fgets(temp, sizeof(temp), stdin);
	} while(sscanf(temp, "%d", &numdev) != 1);

	snprintf(temp, sizeof(temp), "/dev/input/event%d", numdev);
	fd = fopen(temp, "rb");
	if(fd == 0)
	{
		printf("Error. Couldn't open input device.\n");
		return -2;
	}
	numbers = read_numbers(fd, 10);
	fclose(fd);
	if(numbers == NULL)
	{
		printf("Error. Couldn't read from input device.\n");
		return -3;
	}
	min = max = sizeof(numbers[0]);
	printf("\nNumbers:\n");
	for(i=0; i<numbers_to_read; i++)
	{
		if(min > sizeof(numbers[i]))
			min = sizeof(numbers[i]);
		if(max < sizeof(numbers[i]))
			max = sizeof(numbers[i]);
		printf(" %d\n", numbers[i]);
	}
	sprintf(output1, "Index(es) of smallest element(s): ");
	sprintf(output2, "Index(es) of largest element(s):  ");
	for(i=0; i<numbers_to_read; i++)
	{
		sprintf(temp, "%d ", i);
		if(sizeof(numbers[i] == min))
			strcat(output1, temp);
		if(sizeof(numbers[i] == max))
			strcat(output2, temp);
	}
	free(numbers);

	strcat(output1, "\n");
	strcat(output2, "\n");

	printf("Smallest element is %d bytes\n", min);
	printf("Largest element is  %d bytes\n", max);
	printf(output1);
	printf(output2);

	return 0;
}

int list_input_devices()
{
	int fd, count = 0;
	struct dirent* dp;
	char name[100];
	char dev[100];
	char devnum[10];
	DIR* dir;

	dir = opendir("/dev/input");
	if(!dir)
		return 0;

	while((dp = readdir(dir)) != NULL)
	{
		if(dp->d_name && !strncmp(dp->d_name, "event", 5))
		{
			snprintf(dev, sizeof(dev), "/dev/input/%s", dp->d_name);
			fd = open(dev, O_RDONLY);
			if(fd == -1)
			{
				continue;
			}
			if(ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0)
			{
				close(fd);
				continue;
			}
			else
			{
				snprintf(devnum, sizeof(devnum), "%s", dp->d_name+5);
				printf("%s : %s\n", devnum, name);
				close(fd);
				count++;
			}
		}
	}
	closedir(dir);
	return count;
}

int* read_numbers(FILE* fd, int num)
{
	int* ret;
	if(num < 1)
		return NULL;
	ret = malloc(sizeof(int) * num);
	if(fread(ret, sizeof(int), num, fd) != num)
	{
		free(ret);
		return NULL;
	}
	return ret;
}

Last edited by _Mike; 03-14-2010 at 03:37 PM. Reason: typo
_Mike is offline   Reply With Quote
Old 03-14-2010, 03:36 PM   #4
and the hat of Destiny
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 22,495
> I stopped reading there.
Awesome!
I stopped at "....plz,,,,,,"
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 03-15-2010, 01:41 AM   #5
Registered User
 
UltraKing227's Avatar
 
Join Date: Jan 2010
Location: USA, New york
Posts: 123
i stopped at the "help with"
__________________
Macselent Corp
UltraKing227 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
allocation and reallocation of memory dynamically of an integer array zamir C++ Programming 16 05-29-2009 07:25 PM
from 2D array to 1D array cfdprogrammer C Programming 17 03-24-2009 10:33 AM
Array coping into another array?or function returning array baniakjr C++ Programming 6 11-09-2006 03:28 AM
two dimensional dynamic array? ichijoji C++ Programming 6 04-14-2003 04:27 PM
Help with an Array omalleys C Programming 1 07-01-2002 08:31 AM


All times are GMT -6. The time now is 12:11 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22