Thread: Convert int to float in array

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    2

    Convert int to float in array

    The question ask to Write a C program that reads an integer array with M elements (M is read from the user). The program will print the original array with 3 decimal places per value, then count the number of odds and evens in the given array.
    I use Dev-c by the way
    My code
    Code:
    #include<stdio.h>
    
    void main(){
        int M,i,even=0,odd=0;
        int a[M];
        float b[M];
    
        printf("Input array size:");
        scanf(" %d",&M);
        for (i=0;i<M;i++){
            printf("With a[%d]=",i);
           scanf(" %d",&a);        
        }
        printf("Original array:");
        for (i=0;i<M;i++){
            b[i]=(float)a;
           printf(" %.3f",b);
        }
        }
    
    

    The problem:
    if I input 4, the output is ok. But when I input 5 or more the output shows this:
    Input array size:9
    With a[0]=1
    With a[1]=2 ... With a[8]=9
    Original array: 1.000 2.000 3.000 4.000 0.000 0.000 7.000 8.000 9.000
    What is wrong?

    Last edited by Nam Nguyen; 04-21-2018 at 09:43 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    1. main returns int, not void.
    2. C doesn't rewrite the past.
    int a[M];
    float b[M];
    You're using the uninitialized value of M in the declaration, not the value read from the user.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Additionally, shouldn't

    Code:
    printf(" %.3f",b);
    Be

    Code:
    printf(" %.3f",b[i]);
    
    ?

    You also don't need the explicit cast to float in the assignment on the line above that printf

  4. #4
    Registered User
    Join Date
    Apr 2018
    Posts
    2
    Quote Originally Posted by Salem View Post
    1. main returns int, not void.
    2. C doesn't rewrite the past.
    int a[M];
    float b[M];
    You're using the uninitialized value of M in the declaration, not the value read from the user.
    Thank, I already solved the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How i can convert int->float float->int safely?
    By Vanhapolle in forum C Programming
    Replies: 2
    Last Post: 11-04-2014, 08:22 PM
  2. convert float array to int array
    By Svetoslav Bozov in forum C Programming
    Replies: 10
    Last Post: 12-04-2012, 02:29 AM
  3. Replies: 17
    Last Post: 07-26-2012, 09:32 PM
  4. Replies: 8
    Last Post: 07-08-2005, 09:12 AM
  5. Unresolved external 'convert(float, float)'
    By Ipsec Espah in forum C++ Programming
    Replies: 4
    Last Post: 05-21-2003, 10:08 AM

Tags for this Thread