473,324 Members | 2,567 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,324 software developers and data experts.

Urgent Help Required in 'C' Language

Freinds, I want some of you to help me write a program in 'C' to count the Total no. of +ves, total no of -ves and total no of zeros using ternary (Conditional Operaators). I am enclosing the Code fragment htat I have written. But it is giving a "Expression Syntax" Error.

#include <stdio.h>
#include <conio.h>

main()
{
int p, n, z, num, term, i, res ;
p = n = z = 0;
printf("Enter how many numbers you require? ");
scanf("%d",&term);
for (i = 1; i <=term; i++)
{
printf("Enter the number to check");
scanf("%d", &num);
if (num > 0 ? p = p + 1 : If (num < 0 ? n = n + 1 : z = z + 1));
}
printf("Positive numbers = %d\n", p);
printf("Negative numbers = %d\n", n);
printf("zero numbers = %d\n", z);
getch();
}


An Solution to this will be highly appreciated
Oct 3 '06 #1
1 1621
D_C
293 100+
Expand|Select|Wrap|Line Numbers
  1. if (num > 0 ? p = p + 1 : If (num < 0 ? n = n + 1 : z = z + 1));
This is probably where it goes wrong. Typically, if else statements are used instead of question mark and colons. The two following code fragments (should) generate the same code.
Expand|Select|Wrap|Line Numbers
  1. if(expr)
  2.   DoTrue();
  3. else
  4.   DoFalse();
Expand|Select|Wrap|Line Numbers
  1. (expr? DoTrue():DoFalse());
Therefore, either
Expand|Select|Wrap|Line Numbers
  1. if(num > 0)
  2.  p++;
  3. else if (num < 0)
  4.        n++;
  5.      else
  6.        z++;
or
Expand|Select|Wrap|Line Numbers
  1. num>0?p++:num<0?n++:z++;
I'll confess I haven't used the ? : syntax in a long time, and usually it isn't used. I haven't tested the code, but I think it's correct. I know the if else code is.
Oct 3 '06 #2

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

Similar topics

9
by: Milk | last post by:
Hi all, Can anyone help me to do this Question. Coz this is my first time study C++ language and my lecture want me to do this kind of program, i really don't have any ideal pls help me here...
1
by: Milk | last post by:
Hi all, Can anyone help me to do this Question. Coz this is my first time study C++ language and my lecture want me to do this kind of program, i really don't have any ideal, or can send me some...
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
0
by: samir dsf | last post by:
hi i thinks its a very strange problem.I had been running my file ServerList2.aspx and i see the output(when i do f5). i am using this file serverList2.aspx on hte left side of a main page (using...
16
by: | last post by:
Hi all, I have a website running on beta 2.0 on server 2003 web sp1 and I keep getting the following error:- Error In:...
5
by: comshiva | last post by:
Hi all, I have converted my existing ASP.NET project from 1.1 to 2.0 and i have found that everything works fine except the linkbutton control in my datagrid which throws an javascript error when...
1
by: SpiderSwamy | last post by:
Hi, I know little bit about asp, I am facing a problem in Validating the ASP Form.. Example: Stud ID: 501242016 FirstName: Ajit LastName: Kar
8
by: ginnisharma1 | last post by:
Hi All, I am very new to C language and I got really big assignment in my work.I am wondering if anyone can help me.........I need to port compiler from unix to windows and compiler is written...
77
by: Hans Schneider | last post by:
1. in the prg bellow what vars are stored on stack, heap, data segment? int i; void main() { int j; int *k = (void *)malloc(1); }
7
by: new to c | last post by:
Hi! I write the 2 codes int i; i = sizeof(long int); printf("%i", i); i = sizeof(int long); printf("%i", i);
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.