+ 3
Help me with this,everything is written in there
24 Antworten
+ 3
Sample code:
Assuming the variable storing your input is called input
for(int i=0;i<input.length;i++){
for(int j=0;j<numbers.length;j++){
//This loop compares the first character in your input to all the characters in numbers variable
If(input[i]==numbers[j]){
//I program in c++
cout<<alphanum[j];
}
}
}
+ 4
It may help if you gave an example input and the expected output. You are currently printing "from the right". I am not quite sure why you would want to "divide by n-1 zeroes".
But, to get the length of the number in digits, increment a counter every time you divide by ten until division results in zero. You can also use the math library and calculate the logarithm base 10, but that takes more time than dividing like at most nine times by ten.
+ 2
So, you want to count positions from the left, starting from 1, printing in reverse.
Input -> expected output:
1234 -> three one
12345 -> five three one
Like this?
+ 2
If you want to start from the left, I would not even bother using a numeric type but use a string and start processing from the left. Then again, you have not yet had arrays 🤔
+ 2
/* Get the leading exponent of a
* number in base 10 expansion.
* Examples:
* leadexp(1234) == 3
* leadexp(0) == -1
*
* Input: a number n
* Returns: leading exponent, or -1
* if n is zero.
*/
int leadexp(int n) {
int lexp = -1;
while(n != 0) {
n = n / 10;
lexp++;
}
return lexp;
}
+ 2
HungryTradie, your idea will fail for trailing zeroes, e.g. 100000.
+ 2
ANIK, I have rewritten the code in a more verbose fashion. This is an idea of how to count the digits.
You can use the code as is and use the result to produce 1000 from 1234 by multiplying 10s. Or, better yet, try to understand the idea, and use the same to produce 1000 from 1234 directly (it is a minor change 🙂)
+ 2
I always enjoy your unbridled enthusiasm, HungryTradie
+ 2
Hello......
First of all
You can create a variable named numbers storing numbers from 0 to 9
Example....string numbers="0123456789".
Then create another variable array called alphanum(as in numbers in words).
Example alphanum[10]={"zero","one",.......etc}
Array is 10 because of the way array works
Second we need two for loops
First loop will take the first character of your inputted string and compare it with variable numbers.....
Second loop takes the current position of the first loop and compares it with variable numbers if it matches.......you use the current position of the second loop
Example if 0 matches and 0's position is infact zero.....the code will be alphanum[j]
+ 1
for example if its 5421,to get the first value i'll devide it by 1000 and take the result, thats what i meant, 1(n-1) zeros..meaning 1000 in this case,i know how to count but cant get taht part where it'll take the digit and will give me a number!
+ 1
I am not in arrays yet,have to do without it
+ 1
But 56 -> five six is starting from left.
Left side <--- ---> right side
+ 1
What about using getchar() inside a loop, sending every second one to your switch case code? It would probably not be able to have the odd/even correctly, but I haven't thought of a way to start at the left but count odd numbers from the right.
+ 1
What about you copy the input to num2, use a for loop to slice off the right digit (eg: num2/=10; ) then use the increment of that for loop to let you know the number of digits. I'll have a go at that idea.
Then you can use the counter of your loop as your num size.
+ 1
Thanks, yes it will. Maybe Anik can use that loop increment counter for the output loop counter.
+ 1
Right now my head is spinning more than a jet engine !!!🥶considering I am new and having it so hard to understand others
+ 1
G'day Ani Jona 🕊 I think what I have tried now will work!
I'm so sorry ANIK for my unbridled enthusiasm, I didn't intend to cause your brain to explode 🤯
I think my code is only using the things your course has covered. I can't comprehend what your teacher thought you were going to use, but the complicated question isn't simple enough for a beginner to do easily.
https://code.sololearn.com/c9U3IMlIVdrg/?ref=app
+ 1
ANIK
This is what I meant
include <iostream>
#include <string>
using namespace std;
int main(){
//Listing me variables
string sentence;
string numbers="0123456789";
string alphanum[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
//receiving inputs
getline(cin,sentence);
//evaluation
for(unsigned int i=0;i<sentence.length();i++){
for(unsigned int j=0;j<numbers.length();j++){
if(sentence[i]==numbers[j]){
cout<<alphanum[j]<<" ";
}
}
}
}
You can make adjustments to suit you
0
no i want to start from right,i already did that in my code above..
56---five six
0
What about an array that saves each digit, then traverse the array backwards skipping the even numbers?
for(i=0 ;num>0 ;i++){
arr[i]=num%10;
num/=10;
}