Thread: Why is this C program not printing the contents of the array ?

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    10

    Post Why is this C program not printing the contents of the array ?

    Code:
    #include<stdio.h>
    #include<math.h>
    
    cuberoot(float a)     /*A function to find the root*/
    {
        float n;
        n = pow(a,(1/3));
        return n;
    }
    
    main()
    {
        float input_array[20],output_array[20];
        int i;
        
        printf("Give values to the array : \n");    /*Two array are created with 20 elements*/
        
        for(i=0;i<20;i++)
        {
            scanf("%d",&input_array[i]);        /*Their input is taken*/ 
            
            if(input_array[i] == 0)
            {
                break; 
            }
        }
        
        for(i=0;i<20;i++)
        {
            if(i % 2 == 0)
            {
                output_array[i] =  2 * input_array[i];    /*The contents of the array are converted. If the subscript of the element is even then it is doubled*/
            }
            
            else
            {
                output_array[i] = cuberoot(input_array[i]) ;    /*Orelse it's cube root is found*/
            }  
        }
        
        printf("Input Array\tOutput Array\n");
        
        for(i=0;i<20;i++)
        {                                   
            printf("%f\t%f\n",input_array[i],output_array[i]);            /*The contents of the array are printed here */
        }
    }
    But the program is printing all the elements as 0

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    %d doesn't read floats.
    1/3 suffers from integer truncation.
    There's two for you to be getting on with.

    A better compiler might help.
    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
    Registered User
    Join Date
    May 2018
    Posts
    10
    Thanks

  4. #4
    Registered User
    Join Date
    May 2018
    Posts
    10
    Then how should i find the cube root of the number

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing a contents of a file
    By yungcairell2998 in forum C Programming
    Replies: 3
    Last Post: 04-23-2017, 11:51 PM
  2. Printing out contents of bit array
    By sigur47 in forum C Programming
    Replies: 4
    Last Post: 11-22-2011, 04:39 PM
  3. printing array contents
    By c_programmer in forum C Programming
    Replies: 6
    Last Post: 12-15-2006, 06:00 PM
  4. Printing the contents of memory
    By hpteenagewizkid in forum C Programming
    Replies: 6
    Last Post: 11-07-2006, 01:51 PM
  5. printing contents of array vertically
    By richdb in forum C Programming
    Replies: 16
    Last Post: 03-10-2006, 01:12 AM

Tags for this Thread