I am given matrix in this input format:
-1 2 3\n
5 9 11\n
3 4 5\n
So in words it is: N integers where row is ended by user pressing enter (\n in the example) after each row.
I have to check for valid input so if user enters anything besides numbers and white space, (space, new line, tab '\t' and character - for negative integers), it is invalid (including + sign), output of the program should determine if it is NxN matrix.
The way I am thinking is to read it by characters until last read character is '\n' and store them in 2-dimension array (for future use), save number of stored numbers so I can check in the future if it was NxN.
Is this good approach or is there something smarter?
That seems like a decent approach... but you don't need to store them — you just need to check if it either ends before N rows or after N rows.
If the number of entries per row varies, then return false.
If the matrix ends before the number of rows == the number of columns, return false.
If the number of rows surpasses the number of columns, return false.
If it breaks any of your character rules, return false.
EDIT: MY bad, just pulled a dum-dum and realized you actually wanted to store it and not just check it. The same thing applies, but after reading the first row into an array you made of some maximum size, you can allocate a new 2-dimensional char array with malloc, and then just break if it won't fit for some reason.
Related
I only really understand how to do equation in program I need help on how to approach this task, also I not really sure whats its asking of me to do.
Suppose you are given a function with the following declaration:
void printSp(int); /* prints specified number of spaces */
Write a function named printTri that takes a single argument, a character, and returns an integer value. If the character is not a capital letter (between 'A' and 'Z'), then the function simply returns 0. Otherwise, if it is a capital letter, the function will print a triangle of characters that looks like this: A ABA ABCBA ABCDCBA
NOTE: WIth a fixed-width font, the center letter in each row would line up. Write this out for yourself on paper, to figure out how many spaces should be printed on each line before the characters start. The bottom line has zero spaces. How many spaces should the top line have? The letter passed in becomes the highest letter in the triangle. For example, to print the triangle above, the caller passes in 'D'. After printing, the function returns the total number of non-space characters printed. For example, for the example triangle above, the function must return 16. You must call the printSp function, once per line, as part of your solution. (NOTE: Call printSp for every line, even when the number of spaces to print is zero.) History:
This is what I have so far I know its not much but its all I understand how to do.
if (x >='A' && x <= 'Z') printf(" A\n ABA\n ABCBA\nABCDCBA")
else return 0;
The function printSp() prints spaces.
You currently are outputting a hard coded number of spaces instead of using printSp(). Swap printf(" ") for printSp(3)
Reading the question, they want you to output a variable number of rows based on the letter provided. So for D you print the pattern you have hard coded which contains four rows and enough letters to output the D. For E you add another row.
I generally suggest students approach problems like this by starting with hardcoding, as you have. Ensure that you incorporate the required features, like printSP(). Then make the code more generic to handle other inputs.
How i can print for example?
int a = 0145236
When I want to use it, it always gives me a octal number.
Whenever you put zero in front of your number it is printed in octal only. That is the way c works.
However, if you take input from the user and supposing user entered 0123456 it is stored in your variable as 123456 so just don't add 0 in the beginning of your integer number when hard coding.
In case you need to add leading zeros in your number this may help Printing leading 0's in C?
Task:
t denotes the number of inputs, followed by t lines, each containing a single integer n.
For each integer n given at input, display a line with the value of n.
Sample input:
4
1
2
5
3
Sample output:
1
2
5
6
The output should appear after all lines of integer n are taken as input, i.e, it should not display output after each line of input.
How can this be done using a while loop without the use of array to store the input numbers?
while(i<t)
{
scanf("%d",&num);
printf("%d",&num);
i++;
}
This code is working fine if the input numbers n are separated by a space and appears on the same line. But when the input numbers are provided after a newline, it displays the corresponding output after each input value.
Generally such type of input output are used in coding competition where a user is expected to match the expected output to the actual output.
Try pasting the input using a command prompt or online compilers and check it. It is totally fine as the output is as expected.
How does it take if the total input is given at once.
First it reads the t and then it reads the num and prints the number but your printing is actually after the input. This is how the output is checked in a coding competition.
PS: If you want everything after input, use arrays.
If you want to this without using array, then the best way i could think of is recursion. But note, that internally, your values will be stored in the stack frames, and your values will be printed in reverse(because stack is LIFO). Here is how you can do,
void foo(i, t)
{
if(t==i)
return;
int num;
scanf("%d",&num);
foo(++i, t);
printf("%d\n",num);
}
Note, however, that the values will be printed in reverse, as stack is LIFO
I need to create a program where in the first like of input I would put the number of numbers in the string separated by space, and in the second line I would enter them (for example 1 2 3 4 5 6). So I tried using Val, but it can't help me because there are spaces, also I can't use for because the numbers are in one line. Also numbers don't have to be one figure, they are from 1 to 10^9 .
This is the principle: Let s be your string. In a loop to the following
Remove leading spaces in s
while (length(s)>0) and (s[1]=' ') do delete(s,1,1);
Find the first space in the remaining string
p := pos(' ',s);
Copy the non-space part leading in a second string t and remove that part from s
t := copy(s,1,p-1); delete(s,1,p);
get the next array element element[i] with
val(t,element[i],code);
Of course you have to code checks (Is the string s empty after some manipulation? If val gives an error, you have no valid number,...), array indexing etc.
I want to read some numbers from the console. The numbers will appear in this way -
5 1 2 3 4 5
4 5 6 7 8
6 2 3 4 5 6 7
..............
EOF
The starting number represents how many number will appear in that line, i.e., the first number of the first line is 5, so there will be 5 more numbers on this line. The end of input will be denoted by the EOF (end-of-file).
I thought of reading the whole line as a string and then convert them to numbers but I want to know is there any other way to do this.
The 'standard' answer is scanf(). The trouble with the standard answer is that it won't allow you to check that there are the correct number of numbers on a line. So, your idea of reading a line and then converting it piecemeal is much better for error detection.
Take a look at How to use sscanf() in loops; the answer there shows you the basics of what you should do. It isn't an exact duplicate. You will read a first value identifying how many entries are on the line, followed by code to read that many entries into an array, with appropriate diagnostics if the data doesn't match the format claimed for it.
If you are in charge of the data format, you should consider dropping the count field - let the computer count how many values are on the line. Computers are good at counting, and it removes a source of errors to be detected and handled (so it makes the programming easier). (If you do change the input format, your question becomes a duplicate of the linked question.)
Have a look at the documentation of scanf:
int count, current, i;
while(scanf("%d", &count) > 0)
{
for(i = 0; i < count; i++)
{
scanf("%d", ¤t);
// store current
}
}