Page 1 of 1 [ 6 posts ] 

jamiethesilent
Raven
Raven

User avatar

Joined: 11 Jul 2010
Age: 28
Gender: Male
Posts: 100
Location: Mid Wales.

19 Jul 2011, 12:27 pm

#Note: This program should be as per ANSI C. It was designed to awnser this question:

Modify exercise 4 so that it returns the values until the number 99 is enterd, or until 6 even values are enterd. Store the numbers in an array:

/**************************************************************************************************************/
#include <stdio.h>

unsigned int read_in_data(unsigned int sum_of_data); /*The function prototype for the read_in_data() function.*/


unsigned int number_of_runs = 0; /*A variable decleration and assignment statment.
*It will be used to limit the amount of time the while loop can run.*/

int sum; /*Will be the value return by read_in_data() function. Used for if() test.*/

int main(void)
{

while( number_of_runs < 99 )
{

read_in_data(int sum_of_data); /*Call the read_in_data() function.*/

sum = read_in_data(unsigned int sum_of_data); /*sum = the return value of read_in_data();*/

if( sum % 2 != 0 ); /*if sum of the input values is an even number, break. */



}
return 0;
}

unsigned int read_in_data(unsigned int sum_of_data) /*read_in_data does not return a value to the program and has no parameters.*/
{
unsigned int data_store[7]; /*Declare and initialize and single dimensional array with six elements. */
int count; /*Declare a integer type variable to count and limit the amount of data that can be input. */

for( count = 1; count < 7; count++ ) /*While count is < 7, add a value to count and execute the bellow code. */
{
printf( "\nPlease input and even integer value:" );
scanf( "%u", &data_store[count] );
}
sum_of_data = (data_store[2] + data_store[3] + data_store[4] + data_store[5] + data_store[6]);
return sum_of_data;
}

In specific I need help with statment 7, where I want the value returned by read_in_data to be added.

- james


_________________
If we knew what it was we were doing, it would not be called research, would it?
-Albert Einstein

- Cruch Bang Linux.


mcg
Veteran
Veteran

User avatar

Joined: 26 Jan 2010
Age: 34
Gender: Male
Posts: 538
Location: Sacramento

19 Jul 2011, 1:45 pm

You are indexing your arrays wrong.

Code:
unsigned int data_store[7]; /*Declare and initialize and single dimensional array with six elements. */

The code above declares a seven-element array, not a six element array.

Code:
for( count = 1; count < 7; count++ ) /*While count is < 7, add a value to count and execute the bellow code. */
{
    printf( "\nPlease input and even integer value:" );
    scanf( "%u", &data_store[count] );
}
sum_of_data = (data_store[2] + data_store[3] + data_store[4] + data_store[5] + data_store[6]);

Array indexes start at 0 in C, so datastore[7] refers to the 8th element of your array (which is nonexistent).

Code:
read_in_data(int sum_of_data); /*Call the read_in_data() function.*/

sum = read_in_data(unsigned int sum_of_data); /*sum = the return value of read_in_data();*/
About the code above, you can't declare a variable in the arguments of a function call. Since your program doesn't even use the sum_of_data parameter I would have made it a local rather than a parameter, but you can just pass any old int value as in read_in_data(0);.

Once you fix those issues, your code will compile, but I don't think it meets the requirements of the exercise.

I think you were trying to do something like this:
Code:
#include <stdio.h>

int read_in_data(int[]); /*The function prototype for the read_in_data() function.*/


int main(void)
{
   int myEvenValues[6], valueCount, i;

   valueCount = read_in_data(myEvenValues); //populate array with even values from stdin

   printf("You entered the following even values:");
   for(i = 0; i < valueCount; i++)
   {
      printf(" %d", myEvenValues[i]);
   }
   printf("\n");

   return 0;
}

int read_in_data(int myValues[])
{
   int countOfEvensRead = 0;

   //Read until 6 even values are entered or until 99 is entered:
   while(countOfEvensRead < 6)
   {
      int val;
      printf( "\nPlease input an even integer value:" );
      scanf("%d", &val);

      if(val == 99)
         break;

      if(val % 2 == 0)
      {
         myValues[countOfEvensRead] = val;
         countOfEvensRead++;
      }
      else
         printf("Not even. Ignoring...\n");
   }

   return countOfEvensRead;
}



jamiethesilent
Raven
Raven

User avatar

Joined: 11 Jul 2010
Age: 28
Gender: Male
Posts: 100
Location: Mid Wales.

19 Jul 2011, 2:15 pm

Thank you. As you can probably tell, I have only just started learning C. :lol: I had realized the program did not complete the exercise, but posted it anyway so people could help with the debugging. Once again thanks.

One question though:

1. What do you mean by "Populating the array with even values from stdin?

- James


_________________
If we knew what it was we were doing, it would not be called research, would it?
-Albert Einstein

- Cruch Bang Linux.


Last edited by jamiethesilent on 19 Jul 2011, 2:31 pm, edited 1 time in total.

mcg
Veteran
Veteran

User avatar

Joined: 26 Jan 2010
Age: 34
Gender: Male
Posts: 538
Location: Sacramento

19 Jul 2011, 2:23 pm

No problem.

Wait until you get to pointers and memory management. You have a lot of fun ahead of you. :)



mcg
Veteran
Veteran

User avatar

Joined: 26 Jan 2010
Age: 34
Gender: Male
Posts: 538
Location: Sacramento

19 Jul 2011, 6:05 pm

jamiethesilent wrote:
1. What do you mean by "Populating the array with even values from stdin?
Oops, didn't notice this question earlier. By that I just mean I am taking the uninitialized array and filling it with even numbers. The read_in_data function returns the number of evens added to the array so the main function knows how far to look in the array (since the program stops reading after the user enters 99, we might have less than 6 values, in which case the remaining array elements could be anything within the range of int)



jamiethesilent
Raven
Raven

User avatar

Joined: 11 Jul 2010
Age: 28
Gender: Male
Posts: 100
Location: Mid Wales.

20 Jul 2011, 12:41 am

Thank you.

- James


_________________
If we knew what it was we were doing, it would not be called research, would it?
-Albert Einstein

- Cruch Bang Linux.