473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

sum all odds number in array.

i am trying to write a C code which request input from user and store them in array.Number of inputs is determined by the user and it is limited to 100 inputs.Then I need to pass the array to recursive function to sum all odd values in the array.Finally,return the sum of all odd numbers to the user.

However, i got the error which stated that the system cannot find the file specified.
So I am not sure if my code is correct.
I am using Visual C++ express 2005.

Below is my code:

#include<stdio.h>

int sumOdd(int x);
int main ()
{
int arr[100];
int readNum;
int sumAllOdd;
int i;

printf("You may enter up to 100 numbers.\nPlease enter an integer:");
scanf_s("%d",&readNum);

if(readNum>100)
printf("Error.It exceeds 100 numbers.\nPlease enter an integer:");

for( i = 0; i < readNum; i++)
scanf_s("%d",&arr[i]);

sumAllOdd = sumOdd();
}

int sumOdd(int x)
{
int i;
int temp=0;
int arr[100];
for(i=0;i<100; i++)
{
if(arr[i]%2==0)
return 0;
else
temp += arr[i];
return 1;
}
}
Feb 19 '09 #1
14 9680
Banfa
9,065 Expert Mod 8TB
Can not find which specified file? Was this during compilation, linking or running?
Feb 19 '09 #2
when i try to execute the program i got the error "The system cannot find the file specified"
Feb 19 '09 #3
Banfa
9,065 Expert Mod 8TB
Then you have probably got the name or the location of the executable file wrong.
Feb 19 '09 #4
i just click save and build project.then only i execute.which part i went wrong?any suggestion?
Feb 19 '09 #5
weaknessforcats
9,208 Expert Mod 8TB
How are you executing the program? From Visual Studio or from the Command Interpreter?

If it's the Command Interpreter, you have to enter the complete path to the .exe file.
Feb 19 '09 #6
Banfa
9,065 Expert Mod 8TB
Did you check the build result to make sure it actually produced an executable file?
Feb 19 '09 #7
i can execute it already.but there are some errors there.Anyone can help?
how to write it in recursion form?

#include<stdio.h>

int sumOdd(int arr[]);
int main ()
{
int arr[100];
int readNum;
int sumAllOdd;
int temp;
int i;

printf("You may enter up to 100 numbers.\nPlease enter an integer:");
scanf("%d",&readNum);

if(readNum>100)
{
printf("Error.It exceeds 100 numbers.\nPlease enter an integer:");
scanf("%d",&readNum);

for( i = 0; i < readNum; i++)
{
temp += arr[i]
}
}sumAllOdd = sumOdd(2);
}

int sumOdd(int x)
{
int i;
int temp=0;
int arr[100];
for(i=0;i<100; i++)
{
if(arr[i]%2==0)
return 0;
else
temp += arr[i];
return 1;
}
}
Feb 20 '09 #8
JosAH
11,448 Expert 8TB
First fix your iterative version because it's adding garbage values; you defined an array arr local to that function so it's using that array. Better pass the other array as a parameter to that function (and pass its length also).

kind regards,

Jos
Feb 20 '09 #9
im quite new to write a program..After few days break,i try to do the coding once again.But problem is still there.Why my loop keep on repeating instead of jumping to the next statement?Does my recursive function there correct?

#include<stdio.h>

int sumAllOdd(int [],int );
int main (void)
{
int i = 0;
int input;
int arr[100];
char ans;

printf("\nPlease enter amount of integer for input.(Maximum is 100 numbers only\n) : ");
scanf ("%d",&input);

do{
printf("Please enter amount of integer for input.(Maximum is 100 numbers only) : ");
scanf ("%d",&input);

arr[i++] = input;
}while(input>0 && i <=100);


if(input==100)
{
printf("\nMaximum amount of number reached\nThank you.");
printf("\nTotal sum of all odd numbers is %d",sumAllOdd(arr,0));
}
do{
printf("\nDo you still want to continue?( Y to continue or N to quit\n");
scanf ("%c",&ans);

if(ans=='y' || ans||'Y')
{
return main();
}
else if(ans=='n' || ans=='N')
return 0;
else
printf("Invalid input");
return 0;
}while(ans=='y' || ans=='Y' || ans=='n' || ans=='N');

}
int sumAllOdd(int arr[],int x)
{
if(arr[x]==0)
return 0 ;
else if(arr[x]%2)
return arr[x] + sumAllOdd(arr,x+1);
else
return sumAllOdd(arr,x+1);
}
Feb 27 '09 #10
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. do{
  2. printf("Please enter amount of integer for input.(Maximum is 100 numbers only) : ");
  3. scanf ("%d",&input);
  4.  
  5. arr[i++] = input;
  6. }while(input>0 && i <=100);
Your arr array has 100 elements numbered from 0 to 99. This loop will put a value in arr[100] which is outside your array.

Then in sumAllOdd, how does arr[x] get to be zero when the data entry loop excludes values that are zero or less?
Feb 27 '09 #11
solution is found but what if i want to change the do while loop into for loop?any suggestion?
my refinement codes is as below:

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>

int sumAllOdd(int [],int i);

int main (void)
{
double input;
int count;
int arr[100];


printf("Please enter number of inputs:(Max is 100):\n");
scanf ("%lf",&input);

fflush(stdin);

/* do{
printf("<Press 0 to exit>\nInput:");
scanf("%lf",&input);
fflush(stdin);

if( (input/1) == ( (int)input) )
{
arr[count++] = input;
}
else
{
printf("Integer number only.");
count--;
}
}while(input && count<=100);

*/

if(count==100)
printf("Maximum number of input reach.");
printf("\nSum of all odd numbers is %d\n\n",sumAllOdd(arr,0));
}
int sumAllOdd(int arr[],int i)
{
if(arr[i] == 0 || i == 100)
return 0;
else
{
if(arr[i] % 2 == 1)
return arr[i] + sumAllOdd(arr,i+1);
else
return sumAllOdd(arr,i+1);
}



}


@weaknessforcats
Feb 27 '09 #12
weaknessforcats
9,208 Expert Mod 8TB
Maybe:

Expand|Select|Wrap|Line Numbers
  1. input = 1;  //entry values to get you into the loop
  2. count = 0;
  3. while(input && count<=100)
  4. {
  5.     scanf("%d", &input);
  6.     //etc...
  7. }
BTW: Why is input a double but the array you store it in is an array of int?
Feb 28 '09 #13
im casting the input from the user to int.as a preventive way if user key in decimal numbers.
only if it is an integer then it will store it as an array.


@weaknessforcats
Mar 2 '09 #14
weaknessforcats
9,208 Expert Mod 8TB
That's not going to work. The user could type in ABC which isn't a double and scanf will fail.

You can't use scanf if you don't know the format of the input. You need to use a %c or %d or %f or something and that means kyou know ahead of time what is being entered.

If you have no idea what's being ented than you have to use gets and receive th input a s string and then parse the characters inthe string to figure out what was entered.

I recommend for beginners that you stick with giving the program what it expects.

BTW: Your typecast of double to int just suppresses a compiler warning about loss of data as the decimals are chopped off the data. There is no way to convert a double to an int as the int has no decimal places.
Mar 2 '09 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Martin Lucas-Smith | last post by:
I am trying to take a string and split it into a filename and a number where the number is what follows the *last* instance of a comma in that string. Split and explode won't work because if the...
1
by: Reimar Bauer | last post by:
Dear all How can I directly acces the second level of an array. At the moment I am doing something like this. $subset=$ini_array; $number=$subset;
2
by: deko | last post by:
I need to populate a file with past unix time stamps over a period of time specified by the user. The time stamps should be evenly spaced, based on user-defined parameters. My question is: How...
5
by: Eclectic | last post by:
Hey all, I have been using usort to sort my multi dimensional arrays ... function cmp($a, $b){ if($a == $b){ return 0; } return ($a < $b) ? -1 : 1; }
33
by: Steven Bethard | last post by:
Is there a good way to determine if an object is a numeric type? Generally, I avoid type-checks in favor of try/except blocks, but I'm not sure what to do in this case: def f(i): ... if x < i:...
26
by: JGH | last post by:
How can I check if a key is defined in an associative array? var users = new array(); users = "Joe Blow"; users = "John Doe"; users = "Jane Doe"; function isUser (userID) { if (?????)
2
by: _andrea.l | last post by:
I'd like to roder an array: $array $array $array I'd like to order $array by name or by numbre of by a function($array,$array) that return 1 or -1 if id1 is less tha id2 or viceversa... How...
5
by: trifinite | last post by:
I am having difficulty understanding why the following code does not result in error. The code below simply traverses a string, extracting any continuous sequence of digits (0 - 9) and placing them...
3
by: MyMarlboro | last post by:
i wonder am i make a mistake or not... use Data::Dumper; my @array= qw(1 2 3 4 5 6 8 9 2 5); my $highest=@array; foreach my $number (@array) { if (@array > $highest) { $highest = @array;
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.