Below is my code
main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c;
for(j=0;j<5;j++){
printf(" %d ",*p);
++p; }
}
The output was
2 3 4 6 5
How the above code is executed?
it's executed exactly they way you think it's executed: you print the array elements in your for loop, however, you made the array elements as int, therefore, when printing 2.8 which is double, the compiler ignores whatever after the point, means, it sees it as 2 rather than 2.8
When you write the statement :
int c[ ]={2.8,3.4,4,6.7,5};
the decimal values are automatically converted to integers.So the array stores
2,3,4,6,5
So your output becomes as it is.
To work with decimals use float type variables.
float c[ ]={2.8,3.4,4,6.7,5};
The int c[] is integer.You can try it.
float c[] = { 2.8, 3.4, 4, 6.7, 5 };
float *p = c;
for (int j = 0; j<5; j++){
printf(" %f ", *p);
p = p+1;
}
if you use 'float j' instead of 'int j' and '%f' instead of '%d' you would get what you want.
Here due to the using of int j and %d, only the part before the decimal point of each number in the array is printed.
Related
Goodevening everyone, i'm learning now to loop an array of numbers, and i'm just wondering,
how can i square the numbers in my array depending on the input of how many times i should square it
int main() {
int nums[5] = {1, 2, 3, 4, 5};
int loop;
int a;`enter code here`
int pro,pro1,pro2,pro3,pro4;
int spacing = 3;
int i = 0
scanf("%d", &a);
do{
pro = pow(nums[0],2);
pro1 = pow(nums[1],2);
pro2 = pow(nums[2],2);
pro3 = pow(nums[3],2);
pro4 = pow(nums[4],2);
i++
}while (i != a);
printf("%0*d\n", spacing, pro);
printf("%0*d\n", spacing, pro1);
printf("%0*d\n", spacing, pro2);
printf("%0*d\n", spacing, pro3);
printf("%0*d\n", spacing, pro4);
return 0;
}
this is my code so far, i wanted to loop it, and get the results like this:
001
016
081
256
625
or like this
001
256
6561
65536
390625
but i always get this:
001
004
009
016
025
please help me understand thank you
There is a strong error and a number of possible improvements in your code.
The error has already be identified by #SylvainChaugny and is that you re-use original nums value on each and every iteration. The improvements are:
you are using 5 variables pro to pro5 and process them the same. Better to make an array for them, or even better re-use the nums array.
if you want to later extend your program to 6 values, you would have to consistently look through the code to search what needs to be changed: better to use a constant or as you have a literal initialization ask the size to the compiler
you are using pow to process integer values. This is not efficient because as pow takes and returns double values you force a conversion from int to double and back. In addition it might be dangerous for large values: a double has less precision than an int on 64 bits architectures (48 mantissa bits for a double, 64 bits for an int). So it can lead to incorrect results.
So your code could become:
#include <stdio.h>
int main() {
int nums[] = {1, 2, 3, 4, 5};
int len = sizeof (nums) / sizeof (*nums); // idiomatic way for the length of an array
int a;
int spacing = 3;
scanf("%d", &a);
for (int i=0; i<a; i++) {
for (int j=0; j<len; j++) {
nums[j] = nums[j] * nums[j];
}
}
for (int i=0; i<len; i++) {
printf("%0*d\n", spacing, nums[i]);
}
return 0;
}
It is shorter to type (not only laziness but also less sensitive to typos) and it gives the expected result ;-). In addition, if you want to add a value to the array, it can be done be changing one single line.
Your problem is that you always reassign your pro variables to the exact same value (pow(nums[0], 2), pow(nuws[1], 2), ....
So no matter what the value of a is, you always will have your pro3 variable to be equal to pow(nums[3], 2).
You first have to assign the initial values to your pro variables, then use them in the calls to pow(), to be able to square your previous result.
I can't seem to find an answer as to why I am not able to type cast a counting integer into a char type within a for loop. Below is a simple instance in which I have tried to declare the type case before and after the loop begins but have not gotten any where. Fundamentally, I don't understand why this is not allowed and can't, or am too stupid to find any information on this.
#include <stdio.h>
int
main(void){
int i;
//char j; <-If i declare here
//char j=(char)i; <-or here there is no profound affect
for(i=0;i<9;++i){
char j=(char)i;
printf("i=%d, j=%c\n", i, j);} <--Case A: j=%c, Case B: j=%d
return 0;
}
OUTPUT
Case A Output printf("%c",j) (j initialized as char & typecast as char)
i=0, j=
i=1, j=
i=2, j=
i=3, j=
i=4, j=
i=5, j=
i=6, j=
i=7, j=
i=8, j=
Case B Output printf("%d",j) (j initialized as char & typecast as char)
i=0, j=0
i=1, j=1 <- if typecast from int to char worked these values should not
i=2, j=2 <- show up when using printf("%d",j)
i=3, j=3
i=4, j=4
i=5, j=5
i=6, j=6
i=7, j=7
i=8, j=8
I was expecting a printed value of the the number as a char instead of blank values. There could be an issue with terminal or ascii values not being represented properly, but that does not answer the root of my question. ** Case B shows numeric values for the j column. However, these values were printed as using a double place holder. If my type case from double to char had worked, these values would be either the associated ascii values for the numbers or garbage.** I'm not looking for a way around this, just to understand what is going on when the code is compiled or in runtime. Thanks!
The Terminal has maybe a problem with the NUL character when displayed with %c in the formated string. I changed the Program a bit..
#include <stdio.h>
int main(void){
int i, x=123456789;
//char j;
char j=(char)i;
printf("int i: %d\n",i);
printf("int x: %d\n",x);
printf("char j: %d\n",j);
printf("hex j: 0x%02x\n",j);
for(i=32;i<128;++i){
char j=(char)i;
printf("i=%d, j=0x%02x, j= %d, j =%c\n", i, j, j, j);
}
printf("char j: %c\n",j);
// Problems is the output as char?
// Programmers Notepad Consol Specific!
printf("Terminal Stops before this in PN :D\n");
return 0;
}
I think the Program does what you want when printing with %d or %x. But %c makes problems.
I used Programmers Notepad. Other Programs may not have my problem :D
Importand is that Printable characters start at decimal 32 with space. See ASCII Printable. Before that it is normal to get no output. %c tries to convert the decimal value in a printable character according to the ASCII table. Also have a look on the C reference.The %d is for signed decimal Integer.
Because the number you are trying to convert is bigger than a char. Char can only be 1 character, hence you can't fit 9 digits/characters into a single char. This would need to be a char[]/string to do what you are trying.
Or try changing x=0->9 but not more than a single digit.
I'm learning some c now, and must do an exercise for the university. I need an double array which should be filld with int values. So I've this.
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] ) {
int size=0, i=0;
printf("Enter size of array: ");
scanf("%d", &size);
double* field = malloc(size * sizeof(double));
double value;
for(i=0; i<size; i++) {
field[i] = i;;
}
for(i=0; i<size; i++) {
printf("Field%i: %d\n",i, field[i]);
}
free(field);
return 0;
}
But, when I execute it all values are set to 0.
Enter size of array: 10
Field0: 0
Field1: 0
Field2: 0
Field3: 0
Field4: 0
Field5: 0
Field6: 0
Field7: 0
Field8: 0
Field9: 0
printf's %d stands for decimal integer, not double.
So this line:
printf("Field%i: %d\n",i, field[i]);
should be:
printf("Field%i: %f\n",i, field[i]);
Using the wrong format specifier is undefined behavior which you had earlier.
From standard
If any argument is not the correct type for the corresponding
conversion specification, the behavior is undefined.
Also check the return value of malloc. It let's you know whether the allocation failed (returns NULL) or not.
A bit more insight from David C. Rankin's comment
The %d format
specifier was looking for 4-bytes (sizeof int bytes) in memory
ordered based on the endianess of your machine. When you provided an
8-byte (sizeof double) value comprised of a sign-bit, normalized
exponent and mantissa, it had no idea what to do with it.
Well here in printf we can use %f instead of %lf also which has added benefit of compatibility with pre-C99 versions and also it is shorter. (chux pointed this).
I don't think whether malloc function allocated the memory for the array. Check the syntax of malloc and also the return type.
I'm using the following code to get the output up to 5 decimal characters of any number input by user when divided by 1, I have to typecast it with (float).
Can any one tell me how this can be done without typecasting or using float constant?
int main() {
int n;
scanf("%d",&n);
printf("%.5 ", 1/(float)n);
return 0;
}
You can use this piece of code that uses only integers:
printf(n==1?"1.00000":"0.%05d ", 100000/n);
Taking your question strictly literally, you could do:
int main() {
float n;
scanf("%f",&n);
printf("%.5f", 1/n);
return 0;
}
In this code, there is no float literal and no (float) cast.
Technically speaking, this should do it:
printf("%.5f ", 1/pow(n,1));
I believe I have carefully read the entire printf() documentation but could not find any way to have it print out, say, the elements of a 10-element array of float(s).
E.g., if I have
float[] foo = {1., 2., 3., ..., 10.};
Then I'd like to have a single statement such as
printf("what_do_I_put_here\n", foo);
Which would print out something along the lines of:
1. 2. 3. .... 10.
Is there a way to do that in vanilla C?
you need to iterate through the array's elements
float foo[] = {1, 2, 3, 10};
int i;
for (i=0;i < (sizeof (foo) /sizeof (foo[0]));i++) {
printf("%lf\n",foo[i]);
}
or create a function that returns stacked sn printf and then prints it with
printf("%s\n",function_that_makes_pretty_output(foo))
You have to loop through the array and printf() each element:
for(int i=0;i<10;++i) {
printf("%.2f ", foo[i]);
}
printf("\n");
You need to go for a loop:
for (int i = 0; i < sizeof(foo) / sizeof(float); ++i)
printf("%f", foo[i]);
printf("\n");
I don't think there is a way to print array for you in printf. "printf" function has no idea how long your array is.
To be Honest All Are good but it will be easy if or more efficient if someone use n time numbers and show them in out put.so prefer this will be a good option. Do not predefined array variable let user define and show the result. Like this..
int main()
{
int i,j,n,t;
int arry[100];
scanf("%d",&n);
for (i=0;i<n;i++)
{ scanf("%d",&t);
arry[i]=t;
}
for(j=0;j<n;j++)
printf("%d",arry[j]);
return 0;
}