Post Your "c" Program Questions Here

A Member? Please Login  
type your username and password to login
Date: July 27, 2008, 02:10 AM
223944 members and 127271 Topics
Latest Member: frunteefeexia
Nairaland [Nigerian Forum] Home Help Search Who is currently online? Login Register
Nairaland Forum  |  Technology  |  Programming  |  Post Your "c" Program Questions Here
Pages: (1) Go Down Send this topic Notify of replies
Author Topic: Post Your "c" Program Questions Here  (Read 661 views)
r_o_b_b_y (m)
Post Your "c" Program Questions Here
« on: November 01, 2007, 10:54 AM »

You got any questions or problems in C, post them here, and lets tackle it.
Smart K. (m)
Re: Post Your "c" Program Questions Here
« #1 on: November 05, 2007, 01:59 PM »

yea man. i need a c program to convert any number to words in c. thanx in advance. I have developed a similar stuff using VB and c# but its not perfect.

10x in advance.

krazied (m)
Re: Post Your "c" Program Questions Here
« #2 on: November 06, 2007, 04:39 PM »

how do you use the c compiler that is how do you install and load it need help seriously cause am learning this for school
r_o_b_b_y (m)
Re: Post Your "c" Program Questions Here
« #3 on: November 06, 2007, 10:50 PM »

Quote from: Smart K. on November 05, 2007, 01:59 PM
yea man. i need a c program to convert any number to words in c. thanx in advance. I have developed a similar stuff using VB and c# but its not perfect.

10x in advance.


Please shed more lıgth on your questıon. Do you want a program that dısplays the correspondıng letter of the alphabet as a result or what?

Quote from: krazied on November 06, 2007, 04:39 PM
how do you use the c compiler that is how do you install and load it need help seriously cause am learning this for school

Whıch compıler do you use? I use borland. and ınstallatıon ıs not supposed to be a problem. Whats the name of your compıler?
Smart K. (m)
Re: Post Your "c" Program Questions Here
« #4 on: November 07, 2007, 11:13 AM »

Quote
Please shed more lıgth on your questıon. Do you want a program that dısplays the correspondıng letter of the alphabet as a result or what?

The function will take a parameter (double) and return string.

Asumming the signature of the function  is:

  string Convert(double num){
          //implementation
 }


//usage
string myword = Convert(1234);
print("%s", myword);

expected : one thousand two hundred and thirty four.

10x
krazied (m)
Re: Post Your "c" Program Questions Here
« #5 on: November 07, 2007, 02:00 PM »

i use borland or should i say am trying to use borland but i also have a jdk SE i.e. java runtime enviroment installed on my system how do i use it to write programmes thnx Cheesy
r_o_b_b_y (m)
Re: Post Your "c" Program Questions Here
« #6 on: November 08, 2007, 10:44 PM »

@krazıed
just run borland and open 'new' from fıle menu. u are ready to go

@smart k, workıng on ıt.
allonym
Re: Post Your "c" Program Questions Here
« #7 on: November 12, 2007, 07:47 AM »

C/C++ standard library has functions to convert numerical values to strings.  So, I assume that you are not allowed to use it, otherwise, this becomes a very trivial task.

Here is a very simple example of an algorithm to convert the number 115 to a string:

let a = 115
let i = 0
allocate memory for string c
loop until a = 0
   c[i] = a % 10
   a = a / 10
    i = i + 1
end loop
reverse string c


A few things to keep in mind.  The above algorithm as is will only work for integer values.  Pretty much what we are doing is taking advantage of the numerical base (base 10) which means each digit's value is 0-9 * base power.  For example, 115 = 1 x 10^2 + 1 x 10^1 + 5 x 10^0.   The reason this only works for integer values is because an integer divide discards any remainder.  However, since you must deal with values of type double (which are not integers), you'll need to resolve this another way because division of integer/char types and division of double/float types work differently.  Also, doubles can be floating point values, so you'd need to take care of decimal points as well.

I don't like doing people's homework for them so I deliberately did not post code for this.  However, there should be enough info in both the example above for you to figure it out.
Smart K. (m)
Re: Post Your "c" Program Questions Here
« #8 on: November 12, 2007, 05:21 PM »

Quote
Here is a very simple example of an algorithm to convert the number 115 to a string:

let a = 115
let i = 0
allocate memory for string c
loop until a = 0
   c[i] = a % 10
   a = a / 10
    i = i + 1
end loop
reverse string c
Good job (looks fine) but I wonder how the above will produce desired result : one hundred and fifteen.

Quote
I don't like doing people's homework for them
That is 4 people like u who still has homeworks. I left university more than 7 years ago (i am trying to figure out where u were then)

Quote
I deliberately did not post code for this.  However, there should be enough info in both the example above for you to figure it out.
Your thread reads
Quote
Re: Post Your "c" Program Questions Here

Sorry man for disappointing u but i do post codes. Here is the code that converts amount in double to amount in words. I did it in C#. You may copy and use it if u like. It uses Nigeria currency if u like:

public static string AmountInWord(string amount)
        {
            long num;
            long num2;
            string str2;
            string str3;
            string str = "";
            string[] strArray = new string[5];
            amount = amount.Replace(",", "");
            amount = amount.Replace(" ", "");
            str = "";
            try
            {
                double num3 = double.Parse(amount);
            }
            catch
            {
                throw new Exception("The amount supplied is not a number and therefore not convertuble");
            }
            strArray[0] = "Trillion ";
            strArray[1] = "Billion ";
            strArray[2] = "Million ";
            strArray[3] = "Thousand ";
            strArray[4] = " ";
            int index = amount.IndexOf(".");
            if (index > -1)
            {
                str2 = amount.Substring(0, index);
                str3 = amount.Substring(index, amount.Length - index);
                if (str2.Length > 15)
                {
                    throw new Exception("The amount supplied must not be greater than 999 999 999 999 999.99");
                }
                str2 = string.Format("{0:D15}", long.Parse(str2));
                str3 = string.Format("{0:F2}", double.Parse(str3));
            }
            else
            {
                if (amount.Length > 15)
                {
                    throw new Exception("The amount supplied must not be greater than 999 999 999 999 999.99");
                }
                str2 = string.Format("{0:D15}", long.Parse(amount));
                str3 = "0.00";
            }
            amount = str2 + str3.Substring(1, 3);
            int num5 = 0;
            for (int i = 0; num5 <= 4; i += 3)
            {
                if (long.Parse(amount.Substring(i, 3)) > 0)
                {
                    if (long.Parse(amount.Substring(i, 1)) > 0)
                    {
                        num = long.Parse(amount.Substring(i + 1, 1));
                        num2 = long.Parse(amount.Substring(i + 2, 1));
                        if ((num > 0) || (num2 > 0))
                        {
                            str = str + convert(amount.Substring(i, 1)) + "Hundred And ";
                        }
                        else
                        {
                            str = str + convert(amount.Substring(i, 1)) + "Hundred ";
                        }
                    }
                    num = long.Parse(amount.Substring(i + 1, 2));
                    num2 = long.Parse(amount.Substring(i + 1, 2));
                    if ((num >= 20) && (num2 <= 0x63))
                    {
                        num = long.Parse(amount.Substring(i + 1, 1) + "0");
                        num2 = long.Parse(amount.Substring((i + 1) + 1, 1));
                        str = str + convert(num) + convert(num2);
                    }
                    num = long.Parse(amount.Substring(i + 1, 2));
                    num2 = long.Parse(amount.Substring(i + 1, 2));
                    if ((num >= 1) && (num2 < 20))
                    {
                        str = str + convert(amount.Substring(i + 1, 2));
                    }
                    str = str + strArray[num5];
                }
                num5++;
            }
            if (long.Parse(amount.Substring(0, 15)) > 0)
            {
                str = str + "naira ";
            }
            num = int.Parse(amount.Substring(amount.Length - 2, 2));
            if (num > 0)
            {
                num = long.Parse(amount.Substring(amount.Length - 2, 2));
                num2 = long.Parse(amount.Substring(amount.Length - 2, 2));
                if ((num >= 20) && (num2 <= 0x63))
                {
                    num = long.Parse(amount.Substring(0x10, 1) + "0");
                    num2 = long.Parse(amount.Substring(0x11, 1));
                    str = str + convert(num) + convert(num2);
                }
                num = long.Parse(amount.Substring(amount.Length - 2, 2));
                if ((num >= 1) && (num < 20))
                {
                    str = str + convert(amount.Substring(amount.Length - 2, 2));
                }
                str = str + "kobo ";
            }
            return (str + "only").ToLower();
        }

        private static string convert(long amount)
        {
            return convert(amount.ToString());
        }

        private static string convert(string amount)
        {
            switch (int.Parse(amount))
            {
                case 70:
                    return "seventy ";

                case 80:
                    return "eighty ";

                case 90:
                    return "ninety ";

                case 0:
                    return "";

                case 1:
                    return "one ";

                case 2:
                    return "two ";

                case 3:
                    return "three ";

                case 4:
                    return "four ";

                case 5:
                    return "five ";

                case 6:
                    return "six ";

                case 7:
                    return "seven ";

                case 8:
                    return "eight ";

                case 9:
                    return "nine ";

                case 10:
                    return "ten ";

                case 11:
                    return "eleven ";

                case 12:
                    return "twelve ";

                case 13:
                    return "thirteen ";

                case 14:
                    return "fouteen ";

                case 15:
                    return "fifteen ";

                case 0x10:
                    return "sixteen ";

                case 0x11:
                    return "seventeen ";

                case 0x12:
                    return "eighteen ";

                case 0x13:
                    return "nineteen ";

                case 20:
                    return "twenty ";

                case 30:
                    return "thirty ";

                case 40:
                    return "fourty ";

                case 50:
                    return "fifty ";

                case 60:
                    return "sixty ";
            }
            return "";
        }

        public static bool IsNumeric(string val)
        {
            try
            {
                double num = double.Parse(val);
                return true;
            }
            catch
            {
                return false;
            }
        }

Good luck

chukz4real (m)
Re: Post Your "c" Program Questions Here
« #9 on: January 15, 2008, 12:26 PM »

Quote from: Smart K. on November 12, 2007, 05:21 PM
Good job (looks fine) but I wonder how the above will produce desired result : one hundred and fifteen.
That is 4 people like u who still has homeworks. I left university more than 7 years ago (i am trying to figure out where u were then)
Your thread reads
Sorry man for disappointing u but i do post codes. Here is the code that converts amount in double to amount in words. I did it in C#. You may copy and use it if u like. It uses Nigeria currency if u like:

public static string AmountInWord(string amount)
 {
 long num;
 long num2;
 string str2;
 string str3;
 string str = "";
 string[] strArray = new string[5];
 amount = amount.Replace(",", "");
 amount = amount.Replace(" ", "");
 str = "";
 try
 {
 double num3 = double.Parse(amount);
 }
 catch
 {
 throw new Exception("The amount supplied is not a number and therefore not convertuble");
 }
 strArray[0] = "Trillion ";
 strArray[1] = "Billion ";
 strArray[2] = "Million ";
 strArray[3] = "Thousand ";
 strArray[4] = " ";
 int index = amount.IndexOf(".");
 if (index > -1)
 {
 str2 = amount.Substring(0, index);
 str3 = amount.Substring(index, amount.Length - index);
 if (str2.Length > 15)
 {
 throw new Exception("The amount supplied must not be greater than 999 999 999 999 999.99");
 }
 str2 = string.Format("{0:D15}", long.Parse(str2));
 str3 = string.Format("{0:F2}", double.Parse(str3));
 }
 else
 {
 if (amount.Length > 15)
 {
 throw new Exception("The amount supplied must not be greater than 999 999 999 999 999.99");
 }
 str2 = string.Format("{0:D15}", long.Parse(amount));
 str3 = "0.00";
 }
 amount = str2 + str3.Substring(1, 3);
 int num5 = 0;
 for (int i = 0; num5 <= 4; i += 3)
 {
 if (long.Parse(amount.Substring(i, 3)) > 0)
 {
 if (long.Parse(amount.Substring(i, 1)) > 0)
 {
 num = long.Parse(amount.Substring(i + 1, 1));
 num2 = long.Parse(amount.Substring(i + 2, 1));
 if ((num > 0) || (num2 > 0))
 {
 str = str + convert(amount.Substring(i, 1)) + "Hundred And ";
 }
 else
 {
 str = str + convert(amount.Substring(i, 1)) + "Hundred ";
 }
 }
 num = long.Parse(amount.Substring(i + 1, 2));
 num2 = long.Parse(amount.Substring(i + 1, 2));
 if ((num >= 20) && (num2 <= 0x63))
 {
 num = long.Parse(amount.Substring(i + 1, 1) + "0");
 num2 = long.Parse(amount.Substring((i + 1) + 1, 1));
 str = str + convert(num) + convert(num2);
 }
 num = long.Parse(amount.Substring(i + 1, 2));
 num2 = long.Parse(amount.Substring(i + 1, 2));
 if ((num >= 1) && (num2 < 20))
 {
 str = str + convert(amount.Substring(i + 1, 2));
 }
 str = str + strArray[num5];
 }
 num5++;
 }
 if (long.Parse(amount.Substring(0, 15)) > 0)
 {
 str = str + "naira ";
 }
 num = int.Parse(amount.Substring(amount.Length - 2, 2));
 if (num > 0)
 {
 num = long.Parse(amount.Substring(amount.Length - 2, 2));
 num2 = long.Parse(amount.Substring(amount.Length - 2, 2));
 if ((num >= 20) && (num2 <= 0x63))
 {
 num = long.Parse(amount.Substring(0x10, 1) + "0");
 num2 = long.Parse(amount.Substring(0x11, 1));
 str = str + convert(num) + convert(num2);
 }
 num = long.Parse(amount.Substring(amount.Length - 2, 2));
 if ((num >= 1) && (num < 20))
 {
 str = str + convert(amount.Substring(amount.Length - 2, 2));
 }
 str = str + "kobo ";
 }
 return (str + "only").ToLower();
 }

 private static string convert(long amount)
 {
 return convert(amount.ToString());
 }

 private static string convert(string amount)
 {
 switch (int.Parse(amount))
 {
 case 70:
 return "seventy ";

 case 80:
 return "eighty ";

 case 90:
 return "ninety ";

 case 0:
 return "";

 case 1:
 return "one ";

 case 2:
 return "two ";

 case 3:
 return "three ";

 case 4:
 return "four ";

 case 5:
 return "five ";

 case 6:
 return "six ";

 case 7:
 return "seven ";

 case 8:
 return "eight ";

 case 9:
 return "nine ";

 case 10:
 return "ten ";

 case 11:
 return "eleven ";

 case 12:
 return "twelve ";

 case 13:
 return "thirteen ";

 case 14:
 return "fouteen ";

 case 15:
 return "fifteen ";

 case 0x10:
 return "sixteen ";

 case 0x11:
 return "seventeen ";

 case 0x12:
 return "eighteen ";

 case 0x13:
 return "nineteen ";

 case 20:
 return "twenty ";

 case 30:
 return "thirty ";

 case 40:
 return "fourty ";

 case 50:
 return "fifty ";

 case 60:
 return "sixty ";
 }
 return "";
 }

 public static bool IsNumeric(string val)
 {
 try
 {
 double num = double.Parse(val);
 return true;
 }
 catch
 {
 return false;
 }
 }

Good luck



Nice one. Guess it will work. Well, that means with guys like u, I'll excel in my quest to becomin a programmer.
allonym
Re: Post Your "c" Program Questions Here
« #10 on: February 11, 2008, 08:47 PM »

SmartKhave you really been out of school for that long.

Your solution will work, but . . . well, perhaps given my field, I have a preference for very concise solutions :-)

I actually wrote that post while I was working full-time, though I'm back in school. My attitude on doing homework for other people remains the same. Based on my classroom experience and the wording of the question, it is quite obviously a request to do someone's homework.

The category is to "Post your C questions here" not "Do Someone's Homework". Even then, my psuedocode all but gave them the solution, assuming they've been paying attention in class up to this point.
 Programming Friends  Downloaad 47 Ebooks .net  Programming A Converter For *.wma To *.mp3 - By Sbucareer  Page 2
Pages: (1) Go Up Send Topic to Friend by E-mail Reply 
Google
 
Web www.nairaland.com
Sections: TV/Movies (2) Music/Radio (2) Celebrities Jobs (2) Career Romance Books Politics Sports Fashion Travel
Health Schooling Religion General(2) Business Webmaster Programming Computers Phones Cars & Trucks

Links: Page1 Page2 Page3 Page4 Page5 Page6 Page7 Page8 Page9 Page10

Nairaland is owned by Oluwaseun Osewa
Nairaland Forum | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.