Looping through an Array in C with no output - c

Here is my code:
int main()
{
int tiles[9];
int counter=0;
int i=1;
while (counter<8)
{
tiles[counter]=i;
counter=counter+1;
i=i+1;
}
int running_total=0;
int current_number;
printf(tiles);
return 0;
}
But I get no output, what is my problem? I'm new to C so I appreciate any comments/criticism.
Edit: I do get an output, but it's a smily face...

If you want to print a number, you need a format string.
If you want to print an array, you need to loop through it.
int i;
for ( i = 0; i < sizeof(tiles)/ sizeof(tiles[0]); ++i)
printf("%d ", tiles[i]); // << added a space for Dietrich Epp :)

Related

input and print array in C

Why this code doesn't print the full array.How to correct the code or improve to print the full array
int main(void) {
float value[MAX], a;
int bit, i;
int group[10];
bit = 0;
do {
scanf("%f", &a);
value[bit] = a;
bit++;
} while (a == '\n');
for (i = 0; i < bit; i++)
printf("%f", value[i]);
}
Change the stop condition of your reading from
while(a=='\n');
to some int/float value.
As mentioned by someprogrammerdude, variable "a" never be equal to '\n'.

Why isn't this C code's \t working?

I am trying to print a pattern of numbers. Try running this code with a input of 20, you will see that the tab spaces are all in the wrong place, they don't follow the order. I know that the tab spaces jump to the next header, but is there a way to avoid it?
#include <stdio.h>
int main()
{
int i, n, count = 0;
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
printf("%dnumber\t", i);
count++;
if(count == 4)
{
count = 0;
printf("\n");
}
}
return 0;
}
Note: Is there a way to do this with tab spaces only(i.e., "\t" only) instead of using an ordinary white space.
Here is the output that I am getting.
But what I want is this
There are several different problems here.
But it sounds like your main question is "gee: the tabs don't line up like I expect."
SUGGESTED ALTERNATIVE:
Consider using "field length specifiers" in printf():
http://www.cplusplus.com/reference/cstdio/printf/
EXAMPLE:
printf ("%-20s", mystring); // Will always be exactly 20 characters
printf ("%06d", myint); // 6-digits, zero filled
Why not:
#include <stdio.h>
int main( void )
{
int i = 0;
int n = 0;
scanf( "%d", &n );
for( i = 1; i <= n; i++ )
{
printf( "%02dnumber\t", i );
if( i % 4 == 0 )
printf("\n");
}
return 0;
}
Output:
16
01number 02number 03number 04number
05number 06number 07number 08number
09number 10number 11number 12number
13number 14number 15number 16number
Hope it helps!

How can we use multiple "\n" in c programming

In Python,we're using print( "\n" * 15). I wonder that how can we do this in c programming without printf("\n\n\n....\n");. Is there any function for it?
C does not have a string repetition operator, but it does have a function you can use:
char newlines[16];
memset(newlines, '\n', 15); /* <-- this function */
newlines[15] = '\0';
fputs(newlines, stdout);
You can just use a loop:
for (int i = 0; i < 15; i++) {
printf("\n");
}
You can make your own function.
int PrintLines(int n)
{
int x;
for( x =0 ; x<n ; x++ )
{
printf("\n");
}
return 0;
}
Always you can make a loop between the number of new lines you want and make a print.
for(int x=0; x<15; x++){
printf("\n");
}
Call fputc() in a loop.
while (n>0) {
n--;
fputc('\n', stdout);
}
Use "precision". Practical for up to a small number (e.g. up to 20 below)
int precision = 15;
printf("%.*s", precision, "\n\n\n\n\n" "\n\n\n\n\n" "\n\n\n\n\n" "\n\n\n\n\n");

How can I print multiple character with one printf?

I want to print multiple character using printf. My approach up to now is this-
#include <stdio.h>
int main()
{
printf("%*c\n", 10, '#');
return 0;
}
But this only prints 9 spaces before the #.
I want to print it like this-
##########
I am unable to figure out how to do this. Please help me?
You can not use printf like that to print repetitive characters in Ansi C. I suggest you to use a loop like this -
#include <stdio.h>
int main()
{
int i;
for(i = 0; i < 10; i++) putchar('#');
return 0;
}
Or if you have absolutely no desire to use loops, you can do something like this-
#include <stdio.h>
int main()
{
char out[100];
memset(out, '#', 10);
out[10] = 0;
printf("%s", out);
return 0;
}
By the way, using printf like this also works-
#include <stdio.h>
int main()
{
printf("%.*s", 10, "############################################");
return 0;
}
I think the best approach, if you have an upper limit to the number of characters to be output is:
printf("%.*s", number_of_asterisks_to_be_printed,
"**********************************************************************");
I think this will also be the most efficient, portable way to do that.
this will print ten # characters, followed by a newline
char tenPounds[] = "##########";
printf( "%s\n", tenPounds);
I am working on a similar problem in "The C Programming Language" book (exercises 1-13 and 1-14). My own program, to start simply, is to count the occurrences of the digits 0 to 9 in a given input, and print a horizontal histogram made of '=' bars according to each count.
To do this, I created the following program;
main() {
int c, ix, k;
int nDigit[10];
//Instantiate zero values for nDigits array
for(ix = 0; ix < 10; ix++) {
nDigit[ix] = 0;
}
//Pull in input, counting digit occurrences and
//incrementing the corresponding value in the nDigit array
while ((c = getchar()) != EOF) {
if (c >= '0' && c <= '9') {
++nDigit[c-'0'];
}
}
//For each digit value in the array, print that many
//'=' symbols, then a new line when completed
for (ix = 0; ix < 10; ix++) {
k = 0;
while (k <= nDigit[ix]) {
putchar('=');
k++;
}
printf("\n");
}
}
Note that this is a work in progress. A proper histogram should include axes labels, and most importantly this program does not account for digits of zero count. If the input includes five 1's but no 0's, there is no visual way to show that we have no zeros. Still, the mechanism for printing multiple symbols works.

Variable isn't storing return value

I am trying to make a program that converts a hex string into decimal. However I am having an issue assigning a returned integer value from the findLength function. Going by the printf statements I can tell that findLength(theString) will yield the correct value however length is showing a value of 0 despite the fact that I have length = findlength(theString).
This isn't a homework problem, I'm just absolutely stumped as to why this simple assignment isn't working. I've already declared length so I know that's not the issue. I'm also getting no compiler messages. Any help would be greatly appreciated.
Edit: I know convert doesn't do anything useful and the for loop needs to be fixed however that shouldn't be effecting the findLength return right?
Second Edit:
I've always submitted a string of '324' to be tested.
#include <stdio.h>
int convert(char s[], int theLength);
int findLength(char s[]);
int main(){
char theString[100];
int result;
int i;
int length;
printf("%s","Hello, please enter a string below. Press enter when finished.");
scanf("%s",theString); //Apparently scanf is bad but we'll learn better input methods later.
//For my tests I submitted a string of '324'.
length = (findLength(theString)); //length = findLength('324')
printf("%d",findLength(theString)); //yields 3
printf("%d",length); //yields value of 0 always.
result = convert(theString, length);
printf("%d\n result is",result);
return 0;
} //End of main
int convert(char s[], int theLength){ //This function will eventually converts a string of hex into ints. As of now it does nothing useful.
int i;
int sum;
for(i = theLength; i=0; i--){
sum = sum + s[i];
printf("%d\n",sum);
}
return sum;
} //End of convert
int findLength(char s[]){
int i;
for(i = 0; s[i]!='\0'; ++i){
}
return(i);
} //End of findLength
The variable length is storing the correct value. I think what has you confused is how you've laid out your printf statements. If you were to try something like the below it would be much easier to see that your code works.
#include <stdio.h>
int findLength(char s[]);
int main(){
char theString[100];
int result;
int i;
int length;
printf("Hello, please enter a string below. Press enter when finished.\n");
scanf("%s",theString);
length = (findLength(theString));
printf("findLength(theString) = %d\n",findLength(theString));
printf("length = %d\n",length);
return 0;
}
int findLength(char s[]){
int i;
for(i = 0; s[i]!='\0'; ++i){
}
return(i);
}
Just to clarify in your post you have
printf("%d",findLength(theString));
printf("%d",length);
printf("%d\n result is",result);
Note the \n before the %d in the last printf statement. This is 0 because your convert function needs to be fixed and this is the value of result NOT length.

Resources