I know in c, c++, and all other kinds of programming code you can use the syntax "%4d" to print a total of 4 digits. If the variable is only one digit, it will print three 0's and whatever your variable is, if your variable is 2 digits, two 0's and so on. However in Octave it seems that it will print out spaces instead of 0's. Is there a way to force it to print out zeros but keep the specified length of 4 integers?
This is for naming files that the program is outputting so the leading zero's are needed to help organize the data that is outputted.
The place to look is the Matlab documentation: http://www.mathworks.co.uk/help/techdoc/ref/sprintf.html.
But the answer is something like:
sprintf('%04d', 42); % Prints "0042"
Related
I can't understand why this code output is weird. I wrote this out of curiosity and now if I enter 55 it shows a leaf. And also many other things depending on number. I searched it in google but didn't find any possible explanation.
#include <stdio.h>
int main(){
char input='*';
int x;
scanf("%d",&x);
printf("%c",input*x);
return 0;
}
Characters are encoded as integers, usually 8-bit by default in C. Some are defined by ASCII and others depend on your OS, screen font, etc.
The * character has code 42. If you enter 55, your code computes 42*55=2310 and uses the low 8 bits of this value, which is 6, as the character to print. Character 6 is ACK which is not defined as a printable character by ASCII, but it sounds like your system is using something like the legacy IBM code page 437, in which character 6 displays as the spade symbol ♠.
Multiplying a character code by an integer is not a very useful thing to do. I'm not sure what you were expecting to accomplish with this program. If you thought it would print 55 copies of the * character, like Perl's x operator, well it doesn't. C has no built-in way to do that; you would just write a loop that prints * once per iteration, and iterates 55 times.
This is a code I have used to define an array:
int characters[126];
following which I wanted to get a record of the frequencies of all the characters recorded for which I used the while loop in this format:
while((a=getchar())!=EOF){
characters[a]=characters[a]+1;
}
Then using a for loop I print the values of integers in the array.
How exactly is this working?
Does C assign a specific number for letters ie. a,b,c, etc in the array?
What happens when we make an array defined using characters instead of integers in C?
Let's be sure we are clear: you are using integer values returned by getchar() as indexes into your array. This is not defining the array, it is just accessing its elements.
Does C assign a specific number for letters ie. a,b,c, etc in the array?
There are no letters in the array. There are ints. However, yes, the characters read by getchar() are encoded as integer values, so they are, in principle, suitable array indexes. Thus, this line ...
characters[a]=characters[a]+1;
... reads the int value then stored at index a in array characters, adds 1 to it, and then assigns the result back to element a of the array, provided that the value of a is a valid index into the array.
More generally, it is important to understand that although one of its major uses is to represent characters, type char is an integer type. Its values are numbers. The mapping from characters to numbers is implementation and context dependent, but it is common enough for the mapping to be consistent with the ASCII code that you will often see programs that assume such a mapping.
Indeed, your code makes exactly such an assumption (and others) by allowing only for character codes less than 126.
You should also be aware that if your characters array is declared inside a function then it is not initialized. The code depends on all elements to be initially to zero. I would recommend this declaration instead:
int characters[UCHAR_MAX + 1] = {0};
That upper bound will be sufficient for all the non-EOF values returned by getchar(), and the explicit zero-initialization will ensure the needed initial values regardless of where the array is declared.
I have realized the charecter set that can function as an input for getchar() is part of the ASCII table and comes under an int. I used the code following to find that out:
#include <stdio.h>
int main(){
int a[128];
a['b']=4;
printf("%d",a[98]); //it is 98 as according to the table 'b' is assigned the value of 98
}
following which executing this code i get the output of 4.
I am really new to coding so feel free to correct me.
Character values are represented using some kind of integer encoding - ASCII (very common), EBCDIC (mostly IBM mainframes), UTF-8 (backward-compatible to ASCII), etc.
The character value 'a' maps to some integer value - 97 in ASCII and UTF-8, 129 in EBCDIC. So yes, you can use a character value to index into an array - arr['a']++ would be equivalent to arr[97]++ if you were using ASCII or UTF-8.
The C language does not dictate this - it's determined by the underlying platform.
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?
Is there a way to specify the number of spaces using a variable?
For example lets say I have a basic print statement:
printf("%5d",someNumber);
This will print 5 spaces then the number. Is there a way that would let me declare a variable var = 5, and use a variable to determine spacing instead? The reason i'm asking is because I'm trying to control the number of spaces after each iteration of a loop to format data a certain way
Try:
printf("%*d", width, someNumber);
You can find more info by man 3 printf
"%5d" prints at least 5 characters. Leading spaces are first printed as needed, then the '-' sign, if needed, then the digits.
To print an int with at least n characters,use '*', the minimum field width:
printf("%*d", n, someNumber);
To print n spaces only, use
printf("%*s", n, "");
The format string is just that: a string. You can build your own with sprintf() e.g.:
sprintf(frmt,"%%%dd",iterator);
If iterator = 3 wil give the format string "%3d" which you can use in printf() like e.g.:
printf(frmt,some_number);
(The variable field width posted by Jesse Chen is not necessarily available everywhere)