Thread: help with array codes....plz,,,,,,beginner

  1. #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.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by shawndotting View Post
    Write a

    I stopped reading there.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    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

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > 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.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    i stopped at the "help with"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM