Link to home
Start Free TrialLog in
Avatar of jeffiepoo
jeffiepooFlag for United States of America

asked on

C scripting in Unix (newbie question)

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?
Avatar of droyden
droyden

replace printf("%f",c%2);

with
printf("%f", %2);
Avatar of jeffiepoo

ASKER

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
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
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

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.
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
>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.
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.
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
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?
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
>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.
ASKER CERTIFIED SOLUTION
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial