Solved

C scripting in Unix (newbie question)

Posted on 2008-06-21
13
319 Views
Last Modified: 2008-06-23
1.) Here is my program, the error message follows. Supposedly the % is for modulus and is supposed to give me a remainder?
2.) If I did a scanf, and entered any interger or float value, how would I make the program display the last character of the interger. ex:  enter a number: 12   "the last character is 2" .  How would I do this with simple code (as simply as possible)?

#include <stdio.h>

int main(){
float c;
printf("Enter any integer");
scanf("%f", &c);
printf("%f",c%2)
return 0;
}

here is the error message when I try to compile it:  "program2.c:9: error: invalid operands to binary"   Do I need to include another .h file?
0
Comment
Question by:jeffiepoo
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 7

Expert Comment

by:droyden
ID: 21839236
replace printf("%f",c%2);

with
printf("%f", %2);
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 21839279
answer question 2?

so say I had this script with the same problem, how would I fix this script if I have already defined variables a and b as regular integers?

if ((a%2)==0)
 { if ((b%2)==0)
   printf("both%1.0f and %1.0f are even\n",a,b);
   else
   printf("1.0f is even, but %1.0f is odd\n",a,b);}
else
 {if((b%2)==0)
   printf("%1.0f is odd, but %1.0f is even\n",a,b);
  else
  printf("both %1.0f and %1.0f are odd\n",a,b);}


thanks for the help
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 21839288
droyden, when I
replace printf("%f",c%2);

with
printf("%f", %2);

I get the following error message: program2.c:9: error: expected expression before '%' token
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 21839516
You are missing a semicolon on your printf line.
But your main problem is that you can't use the % operator on float variables.

From "The C Programming Language" by Brian W. Kernighan & Dennis M. Ritchie Second Edition (K&R2) page 41:

"The % operator cannot be applied to float or double"

The code below does work, however
#include <stdio.h>
 

int

main()

{

  int c;

  printf("Enter any integer");

  scanf("%d", &c);

  printf("%d", c % 2);

  return 0;

}

Open in new window

0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 21839538
By the way C is a programming language, not a scripting language. The difference is that C is translated to actual machine instructions whereas a scripting language is obeyed line by line by some kind of interpreter. Java sits half-way: it's compiled to byte code (the *same* byte code on *any* platform) but then needs a Java Virtual Machine (jvm, or "run machine") to run it.
Before I answer part 2, please confirm whether this is homework. We experts are not allowed to do your homework for you, but can give you assistance in how you can do it yourself.
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 21840113
Well if you wanted to DO my homework you could check out
http://www.cs.colostate.edu/~cs156/HW1.shtml

But that is not what I want anybody to do. I was hoping I could use this website I pay for to ask questions about things I am getting stuck on. Thank you very much.

So, If I can only use the % (modulus) operator on integers, how do I switch a float variable to an integer variable and keep the variables the same? Say, to change a float a; to an int a; where a keeps the same value?

I have no idea even how to go about the second question with regular addition, subtraction, multiplication, or division. Or even if it is possible.

Thanks for any help --Jeff
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 21840139
>how do I switch a float variable to an integer variable and keep the variables the same? Say, to change a float a; to
>an int a; where a keeps the same value?
Why do you need to use a float? The operations you are performing are valid on an int, your assignment asks you to use ints ... I dont see the need to use floats ...
If you still insist you can simply assign a float to an int
float b = 2.2;
int a = b;
now a holds value 2. The fraction part of the float is simply dropped - this is all assuming that float is not large enough to overflow the int.
Apart from that you can use functions like floor() ciel() round() etc. Check their man pages for more information

Question 2 is simple number system maths

Given any number ab, it can be represented as a*10 + b ... 12 = 1*10 + 2
abc = 100*a + 10*b + c          ....                                           123 = 1*100 + 2*20 + 3
abcd = 1000*a + 100*b + 10*c + d                                       1234 = 1*1000 + 2*100 + 3*10 + 4

kth digit here is  (counting from 1 starting from least significant)
%10^k / 10^(k-1)

Replace 10 with base of any number system that you wish to work with.
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 21840245
I think q1 gives a clue to q2. Use the % operator - a number % 10 is the last digit of that number (to the base 10). % 8 gives the last digit if expressed in octal (to the base 8). % 16 gives the value of the last digit if expressed in hex - but you would need to convert the values 10 - 15 to a -f (or A - F) to get the last digit as a hex digit.
Try changing your original program to use %9 instead of %2 so you can prove it for yourself
As sunnycoder said - why use floats at all? But if you want to input a float and convert it to an integer then possibilities include:

1. cast: this will discard any fractional part. i = (int)f;
2. Round to nearest integer i = (int)rint(f);
Here rint() returns a double which has a value of the nearest integer to its argument. Integer arithmetic is exact even in floating point, so the (int) cast does not lose precision (unless the value is too big to fit: double can hold a 48-bit int exactly (i.e. with exponent zero) but int is only 32 bits).
I suspect you haven't reached these topics in your coursework yet.
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 21840260
You can write i = f as sunnycoder did instead of i = (int)f - the compiler will to the type conversion for you the same either way
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 21841028
ok, if I use %10d it doesn't work, it puts in 10 spaces. If I use %10 it just prints %10 instead of the variable. If I use %d10 it just puts 10 at the end of the number?
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 21841083
Holy crap guys I got it working, thanks for the help. I couldn't have gotten it done without SOME questions answered. The only thing that I did that I wasn't supposed to do is use more than 2 variables. I used int variables so my % operator would work. But I had to make those float variables too so if they averaged out to a number and a half it would actually show it. I wouldn't know how to do this with just int or float variables. I answered my own previous question and I'll assign points in a few days
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 21841101
>But I had to make those float variables too so if they averaged out to a number and a half it would actually show it.
Numbers themselves should be int ... The average that you calculate would be a float. float = (int + int)/2 is valid.
0
 
LVL 34

Accepted Solution

by:
Duncan Roe earned 500 total points
ID: 21842331
I think (int + int) / 2 will use integer arithmetic and thus lose the .5, even though you then assign the result to a float. You have to do (int + int) / 2.0 so everything is floated before doing the arithmetic.
Stay with int for everything except the result.. Total the ints as you read them and keep an integer count of how many you have read. when you go to do the division, cast one of them, e.g. if you have int total for the total and int nr for the number read and float avg for the average:

avg = float(total) / nr

(That's for a putative assignment where you read an arbitrary number of integers terminated by a zero and print the average. Might be best to have total as a float, to avoid integer overflow during the addition).

Usually people use double rather than float, because float is only 32 bits in total so has limited integral range. The hardware only ever does double arithmetic, and converts to the short float format on storing. (May not apply to all architectures)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now