Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,967 members, 7,825,023 topics. Date: Sunday, 12 May 2024 at 12:12 AM

Post Your "c" Program Questions Here - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Post Your "c" Program Questions Here (2304 Views)

Check In: Submit Your C++ Questions Lets Solve / Post Your C#.net Questions Here / Post Ur Vb 6.0 Questions Here (2) (3) (4)

(1) (Reply) (Go Down)

Post Your "c" Program Questions Here by robby1(m): 10:54am On Nov 01, 2007
You got any questions or problems in C, post them here, and lets tackle it.
Re: Post Your "c" Program Questions Here by SmartK1(m): 1:59pm On Nov 05, 2007
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.
Re: Post Your "c" Program Questions Here by krazied(m): 4:39pm On Nov 06, 2007
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
Re: Post Your "c" Program Questions Here by robby1(m): 10:50pm On Nov 06, 2007
Smart K.:

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.


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

krazied:

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 ur compıler?
Re: Post Your "c" Program Questions Here by SmartK1(m): 11:13am On Nov 07, 2007
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
Re: Post Your "c" Program Questions Here by krazied(m): 2:00pm On Nov 07, 2007
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
Re: Post Your "c" Program Questions Here by robby1(m): 10:44pm On Nov 08, 2007
@krazıed
just run borland and open 'new' from fıle menu. u are ready to go

@smart k, workıng on ıt.
Re: Post Your "c" Program Questions Here by allonym: 7:47am On Nov 12, 2007
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.
Re: Post Your "c" Program Questions Here by SmartK1(m): 5:21pm On Nov 12, 2007
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.


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)


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
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(",", ""wink;
amount = amount.Replace(" ", ""wink;
str = "";
try
{
double num3 = double.Parse(amount);
}
catch
{
throw new Exception("The amount supplied is not a number and therefore not convertuble"wink;
}
strArray[0] = "Trillion ";
strArray[1] = "Billion ";
strArray[2] = "Million ";
strArray[3] = "Thousand ";
strArray[4] = " ";
int index = amount.IndexOf("."wink;
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"wink;
}
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"wink;
}
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"wink;
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"wink;
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"wink.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
Re: Post Your "c" Program Questions Here by chukz4real(m): 12:26pm On Jan 15, 2008
Smart K.:

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(",", ""wink;
amount = amount.Replace(" ", ""wink;
str = "";
try
{
double num3 = double.Parse(amount);
}
catch
{
throw new Exception("The amount supplied is not a number and therefore not convertuble"wink;
}
strArray[0] = "Trillion ";
strArray[1] = "Billion ";
strArray[2] = "Million ";
strArray[3] = "Thousand ";
strArray[4] = " ";
int index = amount.IndexOf("."wink;
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"wink;
}
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"wink;
}
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"wink;
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"wink;
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"wink.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.
Re: Post Your "c" Program Questions Here by allonym: 8:47pm On Feb 11, 2008
SmartK. . .have 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.
Re: Post Your "c" Program Questions Here by coreyson(m): 9:44am On Apr 15, 2010
.txt).
thanks.
Re: Post Your "c" Program Questions Here by vincent022: 5:17am On Apr 16, 2010
The new mobile platform
Introduce a new mobile phone platform, all the phones from China, cheap, easy transaction, welcome
www.tootoobuy.com

(1) (Reply)

Female Programmers In Nigeria? / $7 Per Hour Go Developer Job / Chatbot

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 56
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.