-3

I want to get a count of highest number of digits from an array of decimal numbers. For example, between 2.1 and 2.01, the resultant counter should be 2 since there are 2 digits after 2.01. Can anyone please help me with this?

#include<conio.h>
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
    double z[100],x[100],sml;
    int count=0,i=0,n;
    cout<<"ENter number of elements\n";
    cin>>n;
    cout<<"Enter the numbers\n";
    for(i=0;i<n;i++)
    {
        cin>>z[i];
    }
    x[0]=z[0]-int(z[0]);
    i=0;
    for(i=0;i<n;i++)
    while(z[i]>=0.001&&i<n)
    {
        x[i]=z[i]-int(z[i]);
        i++;
    }
    for(i=0;i<n;i++)
    {
        cout<<x[i]<<"\t";
    }
    sml=x[0];
    for(i=0;i<n;i++)
        if(sml>x[i])
            sml=x[i];
    sml=sml-int(sml);
    while(sml>=0.001)
    {
        sml=sml*10;
        count++;
        sml=sml-int(sml);
    }
    cout<<endl<<count;
    return 0;
}
shrys
  • 5,381
  • 2
  • 15
  • 29

3 Answers3

1

It's not impossible, it should be pretty easy actually. Cast it to a string, get the substring of the results starting at the decimal and count the result.

For this you will need to look up:

-casting 
-indexof
-substring

If you give it a try and can't figure out comment and I will offer you a little more guidance but you should try it yourself first.

EDIT:

I don't see much of an attempt to do what I suggested, it looks like you just posted the code you had. So here is some pseudo code for you to work with:

string stringNum =  to_string(decimalNum);
int decimalPos = stringNum.find(".");
string newString = stringNum.substr(decimalPos);
int answer = newString.length();

I pretty well answered it for you, you need to figure out the syntax.

mgrenier
  • 1,240
  • 3
  • 18
  • 39
  • Is it possible to do it the string-way without increasing the time complexity? – shrys Jan 08 '15 at 19:27
  • well any time you are adding operations you are adding complexity...I'm not sure what you mean by "time complexity" – mgrenier Jan 08 '15 at 19:29
  • I just want to use only one for loop and not a tree of for loops(a for loop inside a for loop). – shrys Jan 08 '15 at 19:31
  • @ShreyasGhanate: it is not. Constructing the string will increase the time complexity to the length of the string (which is log(size of number / precision), rougly). – EyasSH Jan 08 '15 at 19:37
  • You need no use any additional loops for this...please make an attempt at it and I would be glad to help you if you can't figure it out. I would rather help you learn then provide the answer and you learn nothing. – mgrenier Jan 08 '15 at 19:39
1

just go ahead and use this:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main(){
float number[] = {1.234,5.64,2.001,7.11112,3.999};

int a,numAfterDecimal  = 0;
    for(a=0;a<sizeof(number)/sizeof(*number);a++){
        ostringstream buff;
        buff<<number[a];
        string numStr= buff.str();
        int pos = numStr.find(".");
        string floatStr = numStr.substr(pos+1);
        if(a == 0){
           numAfterDecimal = floatStr.length();
        }
        else if(floatStr.length() > numAfterDecimal){
           numAfterDecimal = floatStr.length();
        }
    }
cout << " higest number of digit after decimal is:"<< numAfterDecimal <<endl ;
}
hemraj
  • 854
  • 6
  • 14
  • can you please explain me complexity of above program? – shrys Jan 10 '15 at 19:39
  • if we replace the part of code inside for loop with a function assume highest_decimal(), we can say that for loop calls the function highest_decimal() n times where n = size of number[] i.e 5 over here.So Complexity is O(5) i.e O(n) – hemraj Jan 11 '15 at 14:05
1

Answer is already accepted. But just for the fun of it. Here a solution using C++ algorithms.

This will reduce the number of statements in main drastically.

Maybe it helps you to better understand modern C++

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>

inline size_t getNumberOfDigitsForFraction(const std::string& s)
{
    size_t positionOfDecimalPoint = s.find("."); // Look for decimal point 
    // And count the numbers of digits after the decimal point
    return positionOfDecimalPoint == std::string::npos ? 0 : s.substr(positionOfDecimalPoint+1).size();
}

int main()
{
    std::cout << "Enter the number of elements that you want to check:  ";
    size_t numberOfElementsToCheck{0}; 
    // Read how many data the user wants to process
    std::cin >> numberOfElementsToCheck;

    // Hier we will store the values    
    std::vector<std::string> elements(numberOfElementsToCheck); 
    // Copy all wanted values from std::cin
    std::copy_n(std::istream_iterator<std::string>(std::cin),numberOfElementsToCheck,elements.begin());

    // Get the Element with maximum digits and print the number of digits
    std::cout << "Max number of digits following decimal point:  " <<
        getNumberOfDigitsForFraction(
        *std::max_element(elements.begin(), elements.end(), 
        [](const std::string &sLeft, const std::string &sRight)
        { return getNumberOfDigitsForFraction(sLeft) < getNumberOfDigitsForFraction(sRight);} )) << '\n';

    return 0;
}

Armin Montigny
  • 7,879
  • 3
  • 11
  • 29
  • Though my question is trivial, the answers are pretty amazing. I find myself refer them now and then. Thanks! – shrys Jun 28 '19 at 14:22