C program - Help with debuging.
#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.
You are indexing your arrays wrong.
The code above declares a seven-element array, not a six element array.
{
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).
sum = read_in_data(unsigned int sum_of_data); /*sum = the return value of read_in_data();*/
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:
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;
}
Thank you. As you can probably tell, I have only just started learning C. 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.
Similar Topics | |
---|---|
What Trump’s Win Could Mean For Medicaid, Disability Program |
08 Nov 2024, 12:53 pm |
Decided to quit PhD because program did not accommodate me |
29 Nov 2024, 9:38 pm |