|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Need Programming Help ASAP
Hey,
Im taking a programming class and this program has gotten me stuck if anyone can help me please do Write for loops that accomplish the following tasks: 1) Let the user input a String and an integer. Then use a for loop to print the String that number of times. Example: Input a string: Hello Input an integer: 3 Output: Hello Hello Hello 2) Let the user input an integer. Then use a for loop to sum the even, odd and all of the numbers between 0 and the input number (inclusive). HINTS: the input number could be negative. you will need 3 sum variables to keep track of the different sums. Example: Input an integer: 10 Sum of the even numbers from 10 to 10 = 30 Sum of the odd numbers from 0 to 10 = 25 Summ of all the numbers from 0 to 10 = 62 3) Let the user input a String and use a for loop to count the number of vowels (a, e, i, o, u) & consonants in the String and print the results. Example: Input a string: Hello Number of vowels in Hello = 2 Number of consonants in Hello = 3 |
|
#2
|
|||
|
|||
|
Answer for first one
Code:
<%
strDataString=request.form.item("txtString")
strDatanoTimes=request.form.item("txttimes")
For lIntI=1 to strDatanoTimes
response.write strDataString & " "
Next
%>
|
|
#3
|
|||
|
|||
|
start a new C# Console application named Program.
Then insert this into Program.cs. Be sure to test it, then convert it to ASP. Good luck. Code:
using System;
using System.Collections.Generic;
using System.Text;
class Program
{
static void Main(string[] args)
{
Selector();
}
private static void Selector()
{
string sRepeat;
string sFunction;
Console.WriteLine("Choose a program");
Console.WriteLine("1: String Repeater");
Console.WriteLine("2: Sums");
Console.WriteLine("3: Character Counter");
sFunction = Console.ReadLine();
switch (sFunction) {
case "1":
{
F1();
break;
}
case "2":
{
F2();
break;
}
case "3":
{
F3();
break;
}
}
Console.Write("\nWould you like to try again?[Y/N]");
sRepeat = Console.ReadLine();
if (sRepeat.ToLower() == "y")
{
Selector();
}
}
private static void F1()
{
//F1 - Repeat string
//(no parameters)
int iNumberOfWrites;
string s;
try
{
Console.WriteLine("String to repeat:");
s = Console.ReadLine();
Console.WriteLine("Number of times to print it:");
iNumberOfWrites = Convert.ToInt32(Console.ReadLine());
}
catch (Exception e)
{
Console.WriteLine("The following error occurred:\n{0}", e.Message);
Console.WriteLine("Invalid Input.");
return;
}
for (int j = 1; j <= iNumberOfWrites; j++)
{
Console.WriteLine(s);
}
}
private static void F2()
{
//F2 - SumTotal, SumOdd, and SumEven
//(no parameters)
Int16 iNumberInput;
long iSumTotal =0;
long iSumEven =0;
long iSumOdd=0;
bool isOdd;
try
{
Console.WriteLine("Enter your number:");
iNumberInput = Convert.ToInt16(Console.ReadLine());
}
catch (Exception e)
{
Console.WriteLine("Invalid Input");
return;
}
// Math works much better...
iSumTotal = (iNumberInput+1) * iNumberInput / 2;
iSumEven = Convert.ToInt32(Math.Pow(iNumberInput / 2, 2) + iNumberInput / 2);
iSumOdd = Convert.ToInt32(Math.Pow((iNumberInput+1) / 2, 2));
//Adjust for negative input
iSumTotal = Math.Sign(iNumberInput) * iSumTotal;
iSumOdd = Math.Sign(iNumberInput) * iSumOdd;
iSumEven = Math.Sign(iNumberInput) * iSumEven;
iSumTotal = 0; //resetting because they're used below
iSumOdd = 0; //resetting because they're used below
iSumEven = 0; //resetting because they're used below
//...but here goes the hard way.
isOdd = true;
for (int j = 1; j <= Math.Abs(iNumberInput); j++)
{
iSumTotal = iSumTotal + j;
// start at 1, which is odd
iSumOdd = iSumOdd + j * Convert.ToInt32(isOdd);
iSumEven = iSumEven + j * Convert.ToInt32(!isOdd);
// flip boolean
isOdd = !isOdd;
}
//Adjust for negative input
iSumTotal = Math.Sign(iNumberInput) * iSumTotal;
iSumOdd = Math.Sign(iNumberInput) * iSumOdd;
iSumEven = Math.Sign(iNumberInput) * iSumEven;
Console.WriteLine("Sum Total: {0}", iSumTotal);
Console.WriteLine("Sum Even: {0}", iSumEven);
Console.WriteLine("Sum Odd: {0}", iSumOdd);
}
private static void F3()
{
//F3 - Count Vowels and Consonants
//(no parameters)
int iNumVowels = 0;
int iNumConsonants = 0;
string s;
try
{
Console.WriteLine("String to parse:");
s = Console.ReadLine().Trim().ToLower();
}
catch (Exception e)
{
Console.WriteLine("Invalid Input.");
return;
}
//Loops are not the easiest way...
//iNumVowels = s.Split(new char[] { 'a','e','i','o','u' }).Length - 1;
//iNumConsonants = s.Split(new char[] { 'b','c','d','f','g','h','j','k','l','m','n','p','q ','r','s','t','v','w'}).Length - 1;
//...but we'll use them anyway.
for (int j = 0; j < s.Length; j++)
{
if (s.Substring(j, 1).IndexOfAny(new char[] {'a','e','i','o','u' }) == 0)
{
iNumVowels++;
}
else if (s.Substring(j, 1).IndexOfAny(new char[] { 'b','c','d','f','g','h','j','k','l','m','n','p','q ','r','s','t','v','w'}) == 0)
{
iNumConsonants++;
}
}
Console.WriteLine("There are {0} vowels and {1} consonants.", iNumVowels, iNumConsonants);
}
}
|
|
#4
|
|||
|
|||
|
If you want this in ASP then you can use an array function
http://www.aspisfun.com/code/parse/letterfreq.html
__________________
CyberTechHelp |
|
#5
|
|||
|
|||
|
Okay, here is some help
1) This one is simple. Like baby simple. If you don't know how to do this you're going to have a really hard time passing your class. I suggest you sit down with the instructor for some extra tutoring so you can better understand the concepts. 2) You're going to need a loop that steps through every number, starting at 0 and working up to the number entered. If the number is odd, you add it to your sum of odd numbers. If it's not odd, then it's even and you need to add it to your sum of even numbers. You will aslo need to add it to the total sum, regardless of if it's even or odd. 3) Similar to #2, except you need to step through each character in the entered string. Check to see if the character is a vowel, and if so you add one to your counter. |
![]() |
| Viewing: ASP Free Forums > Other > Programming Help > Need Programming Help ASAP |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|