I have homework due that states that I need to write a program that generates the first 15 letters of the english alphabet. I can't delcare and set 15 different variables or constants. The letters must be displayed in a number of columns initially set by the user. the numbers have to be aligned in columns. Can anyone help? Maximum number of columns is 7 and the minimum is 1.
Here's some pseudo-code to get you started. Read it, understand it, then try to implement it.
get numcols from user
if numcols < 1 or numcols > 7:
print error and exit
ch = 'a'
for count = 1 to 15:
output ch followed by space
add 1 to ch
if count is an integral multiplier of numcols:
output newline
endif
endfor
if numcols is not equal to 3 or 5:
output newline
endif
It's pitched at about the level of your homework (no fancy stuff and the smallest hint of awkwardness) and should map reasonably well into C code.
As part of this implementation, you should research:
the fact that character constants like 'a' are really integers in disguise.
remainder or modulus (%) operators and how/why they are useful here.
getting user input with scanf.
putchar for outputting characters.
why you have that final if statement :-)
Here is a hint:
ASCII code of A is 65, B is 66, C is 67 and so on. You can do it in a loop starting from 65 and going on for 15 iterations.
This can be done with two nested loops, one for the vertical and one for the horizontal. since the numbers are in sequence in value you can increment the variable for the character each time.
I don't want to give away more than that unless another user says I should. I've already given a lot of help and I'm sure you can figure out the rest.
If you feel you need more help I'll try to not give too much but explain more.
Related
I come to this site in need of help, after struggling with this problem for a few days now. I am trying to program a poem that accepts some data from standard input and then outputs a poem based on that data.
The code seems to be working, but it is not correct! It is giving me the wrong index of the array I am using. I would love extra eyes to help me with my code and let me know what I am doing wrong.
ALSO! For some reason, I am not able to access the third array of the char array... I tried to place "SIZE - 1" in there but it prints nothing... Would love to understand why this is. Does this look right?
// Program that accepts some data from standard input,
#include <iostream>
#include <cstring>
//here... extracted.
for (int sign = 0; sign < poem[line]; sign++)
{
if (line > word_count)
{
std::cout << " ";
print_poem(seed);
}
else
{
print_poem(seed);
}
You haven't mentioned what exactly the task is but I can at least explain to you parts of the problem.
Are the correct syllables being printed?
Let's assess if the correct syllables are being printed. I ran your code on my machine (with the input you provided that is "100 3 1 5 7 5") and got:
nahoewachi
tetsunnunoyasa
munahohuke
The syllable count of each line is fine (5,7,5) so that's not a problem.
The first syllable you have a problem with is chi in nahoewachi. I'm only illustrating why this syllable is being printed. You can apply the same logic to the rest.
Initially, the seed is 100. Before processing the first row, you apply generate_prnd, which gives 223. Before calculating chi, you print 4 other syllables (na, ho, e and wa). This means that you have applied generate_prnd 8 times before calculating the fifth syllable.
Applying generate_prnd 8 times on 223 gives 711. Applying one more time (to get row) gives 822.
822%9 = 3rd row (0 indexed).
Applying one more time (to get column) gives 361. 361%5 = 1st column.
Therefore the index for the fifth syllable is (3,1). The string at the (3,1)th index is "chi". Therefore, the correct syllable is being printed. The indexing is correct. There's a problem with your logic if you want a different syllable to be printed.
Now, let's assess why there aren't any spaces in your output.
In the example you provided, num_lines=3. The word_counts (actually syllable counts) are 5, 7 and 5. You are applying a space when line (which is always less than num_lines) is greater than word_count.
However, line is always less than word_count since the maximum value of line is 2 (num_lines - 1). Therefore, a space will never be printed.
P.S. If you are allocating memory using new, don't forget to deallocate using delete later.
I am working on a project and as I have not coded with Fortran before, I am struggling a lot. My professor gave me a code file which I need to fix but I don't understand the syntax.
So, in the file he has
g = some formula,
1 some formula
2 * some formula
3 / some formula.
What does 1, 2, 3, * and / do?
I asked my Professor, and he said that this is Fortran 77 code and 1, 2, 3 are used as indexing in column 6 and the g is in column 7 as that's how the Fortran code is written. But I was very confused why Fortran 77 only accepts code after column 7?
Thank you for all the replies.
What you are most likely looking at is Fixed source-form statement continuation which is part of the Fixed source form.
Fixed-form formatting is an old way of formatting code which still stems from the old punched-cards. Lines could only be 72 characters long, but sometimes you needed more. Hence, the statement-continuation character:
Except within commentary, character position 6 is used to indicate continuation. If character position 6 contains a blank or zero, the line is the initial line of a new statement, which begins in character position 7. If character position 6 contains any character other than blank or zero, character positions 7–72 of the line constitute a continuation of the preceding non-comment line.
source: Fortran 2018 Standard, Section 6.3.3.3
Which character is used as statement-continuation marker, is up to the programmer and his style. Often you see a <ampersand>-character (&), or <dollar>-character ($) or the <asterisk>-character (*) like so:
c23456789012345678901234567890123456789012345678901234567890123456789012
g = something long
& + something_longer
& + something_even_longer
However, in the really old days, people often numbered their lines.
c23456789012345678901234567890123456789012345678901234567890123456789012
0g = something long
1 + something_longer
2 + something_even_longer
and because space was limited, they removed all spaces, which sometimes becomes very confusing when you have numbers in your line:
c23456789012345678901234567890123456789012345678901234567890123456789012
0g=1.2345+
10.35697-
22.5789
This does not add 10.35697 and subtract 22.5789, but adds 0.35697 and subtracts 2.5789
The usage of numbers as statement continuation markers is again inherited from the punched-cards. A single punched-card represented a single Fortran statement. And on the card, the row and column numbers were printed (Thanks to High Performance Mark for this information)
Note: the asterisk and slash in the OP are nothing more than the normal multiplication and division.
I initially learned that if I want to see if a cell has any contents to use if(A1<>"",.... But as I receive more and more assistance on SO, it seems most people use if(LEN(A1),... Is there a difference? Did I learn the wrong information? Should I ever opt for one over the other or just always use LEN from now on?
pretty much the same result. difference is:
LEN(A1) - checks if A1 has a length
A1<>"" - checks if A1 is not equal to "empty"
then there is a length of the formula itself (some prefer to save 1 extra character space):
A1<>"" has 6 characters compared to LEN(A1) 7 characters
the superiority of LEN comes when you need to check for character count like:
=IF(LEN(A1)=4, TRUE, FALSE)
eg. output TRUE only if A1 value has exactly 4 characters
So I have some code that does essentially this:
REAL, DIMENSION(31) :: month_data
INTEGER :: no_days
no_days = get_no_days()
month_data = [fill array with some values]
WRITE(1000,*) (month_data(d), d=1,no_days)
So I have an array with values for each month, in a loop I fill the array with a certain number of values based on how many days there are in that month, then write out the results into a file.
It took me quite some time to wrap my head around the whole 'write out an array in one go' aspect of WRITE, but this seems to work.
However this way, it writes out the numbers in the array like this (example for January, so 31 values):
0.00000 10.0000 20.0000 30.0000 40.0000 50.0000 60.0000
70.0000 80.0000 90.0000 100.000 110.000 120.000 130.000
140.000 150.000 160.000 170.000 180.000 190.000 200.000
210.000 220.000 230.000 240.000 250.000 260.000 270.000
280.000 290.000 300.000
So it prefixes a lot of spaces (presumably to make columns line up even when there are larger values in the array), and it wraps lines to make it not exceed a certain width (I think 128 chars? not sure).
I don't really mind the extra spaces (although they inflate my file sizes considerably, so it would be nice to fix that too...) but the breaking-up-lines screws up my other tooling. I've tried reading several Fortran manuals, but while some of the mention 'output formatting', I have yet to find one that mentions newlines or columns.
So, how do I control how arrays are written out when using the syntax above in Fortran?
(also, while we're at it, how do I control the nr of decimal digits? I know these are all integer values so I'd like to leave out any decimals all together, but I can't change the data type to INTEGER in my code because of reasons).
You probably want something similar to
WRITE(1000,'(31(F6.0,1X))') (month_data(d), d=1,no_days)
Explanation:
The use of * as the format specification is called list directed I/O: it is easy to code, but you are giving away all control over the format to the processor. In order to control the format you need to provide explicit formatting, via a label to a FORMAT statement or via a character variable.
Use the F edit descriptor for real variables in decimal form. Their syntax is Fw.d, where w is the width of the field and d is the number of decimal places, including the decimal sign. F6.0 therefore means a field of 6 characters of width with no decimal places.
Spaces can be added with the X control edit descriptor.
Repetitions of edit descriptors can be indicated with the number of repetitions before a symbol.
Groups can be created with (...), and they can be repeated if preceded by a number of repetitions.
No more items are printed beyond the last provided variable, even if the format specifies how to print more items than the ones actually provided - so you can ask for 31 repetitions even if for some months you will only print data for 30 or 28 days.
Besides,
New lines could be added with the / control edit descriptor; e.g., if you wanted to print the data with 10 values per row, you could do
WRITE(1000,'(4(10(F6.0,:,1X),/))') (month_data(d), d=1,no_days)
Note the : control edit descriptor in this second example: it indicates that, if there are no more items to print, nothing else should be printed - not even spaces corresponding to control edit descriptors such as X or /. While it could have been used in the previous example, it is more relevant here, in order to ensure that, if no_days is a multiple of 10, there isn't an empty line after the 3 rows of data.
If you want to completely remove the decimal symbol, you would need to rather print the nearest integers using the nint intrinsic and the Iw (integer) descriptor:
WRITE(1000,'(31(I6,1X))') (nint(month_data(d)), d=1,no_days)
I want to write a program that can give me all 4 letter words (from the dictionary or outside the dictionary). I code in C++. And by far, I've reached nowhere.
I'm simply a beginner in C++, I can apply the logic but I'm not introduced to advanced features in C++. It doesn't matter if it takes a long time for this program to end execution, I just want the solution.
For example:
abcd
king
ngik
cbda
play
lpay
payl
and so on (just a few of the millions of outputs I hope this program to output).
NOTE: The words generated need not make sense and I do not want to discard any combinations, I want it all.
I suggest looping say i from 0 to 26^4 - 1, each time outputting 'A' + i / (26*26*26), 'A' + i / (26*26) % 26, 'A' + i / 26 % 26, and 'A' + i % 26, then a newline.
Make a array that has all the possible letters in it(add numbers and symbols if you want).
Then use four nested for loops that loop a number from 0 to the length of the array.
Lets say that the loop number variables are a,b,c,d.
In the inner loop(the last one) you can output it as array[a] + array[b] + array[c] + array[d]
This gives all the possible combinations where you can add numbers and symbols aswell.
use recursion
explore DFS a 26-ary tree and output the letter every time you go down on the corresponding branch. depth = 4.
P.S. recursive algorithms eat stack memory like hell... so be sure your machine let programs have enough stack mem. You don't wanna run this on a pic micro with only 3-level call stack :-)