Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5) multiple floating points aligned by the decimal point in C programming, only

ID: 3589551 • Letter: 5

Question

5) multiple floating points aligned by the decimal point in C programming, only allow to use %d, %c for printf, use if, if-else and while loop.

a)read in x number of floating points, where x is the positive integer(assume the number is less than 20). You can assume that the user will enter x floating points separated by spaces.

b)once all of the x floating points have been entered, they should be printed out with decimal points aligned.

For input 45 . 678, 4567 . 8, 4 . 5678. -12, 0 . 3456789, 456. 78. the output will be: 45.678 4567·8 4.5678 -12.0 0.3456789 456.78

Explanation / Answer

Note: as i compiled in dev c++ the ouput is shown below according to requirement . as iam taking values in float array if you give more than 3 values after the decimal it will display three vaulues only .

#include<stdio.h>

#include<math.h>

int count;

int main()

{

char buf[20];

int val,i=0,k=0; //declaring the variables

getval:printf("enter the number of values you want:"); // label named getval and follwoing print statement

scanf("%d",&val); //scanning the value from user

float dec[val];

if( val>=20) // as you said the user canot enter greater than 20

{

printf("please enter the values less than 20 ");

goto getval;  

}

printf("enter the decimal values :");

while( val>=0) // as you while loop is used

{

scanf("%f",&dec[i]); //storing the elements in array

i++; // incrementing value of i

val--; //decrementing value of val  

}

int n = sizeof(dec);

printf("the output values are :");

for (i=0 ; i<=n/sizeof(int);i++)

{

int dot=0;

int j;

gcvt(dec[i],6,buf); // this is standard function which converts float to string

  

for(j=0;j<=sizeof(buf);j++)

{

if(buf[j]!='.'&& dot!=1 )

{

count++;

}

if(buf[j]=='.')

{

dot=1;

}

}

printf("%s ",buf);

count=0;

}

}

output:

enter the number of values you want:3
enter the decimal values :10.986 1.3 1098.8 -12
the output values are :

10.986
1.3
1098.8
-12.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote