Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an array board[3][3] of type int. I want to show 8 numbers and on the last place "_" must be shown. I don't know how to do that. Please help
If the 8 numbers you want print is the first 8, you can just use a loop to with printf to print it until the last number:
int j, k;
for (j = 0; j < 3; j++) {
for (k = 0; k < 3; k++) {
if (j == 2 && k == 2)
printf("_");
else
printf("%d ", board[j][k]);
}
printf("\n");
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am new to programming and have just started with array. This was my code for the problem
#include <stdio.h>
int main(){
int i=1,f=1,num,fac[10],sum=0;
for (num=1; num<=10; num++) {
for (i; i<=num; i++) {
f = f * i;
}
fac[num-1]=f;
}
for(i;i<=9;i++)
sum = sum + fac[i] * fac[i+1];
printf("The sum is %d",sum );
return 0;
}
Output it is giving is-The sum is 0
So what are the corrections to be made or any other code for the problem?
You need to reinitialize the variable i more often.
Instead of having a for loop that looks like for (i; i<=N; i++), initialize i and do for (i=0; i<=N; i++)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
It took me all day to figure this out but even though I completed it I don't fully understand how it works and I feel there is a better or cleaner way of writing it.
Can some one explain how I can improve my code?
#include<cs50.h>
#include<stdio.h>
int main(void)
{
int rows, height, spaces, hashes;
do
{
printf("Height: ");
height = get_int();
}
while(height < 0 || height > 23);
for(rows = 0 ; rows < height; rows++)
{
for(spaces = height - 1; spaces > rows; spaces--)
{
printf(" ");
}
for(hashes = 0; hashes < spaces + 2; hashes++)
{
printf("#");
}
printf("\n");
}
return 0;
}
It is best if you first try to understand what the code is doing. Work it through on paper, starting with the input 1.
If you are keen you might like to explore the printf width and precision formatting, where each * in the format specifier is replaced by the corresponding function argument. But beware - printf is a complicated and detailed function. This also introduces the array.
char hatch[] = "##############################";
for(rows = 0; rows < height; rows++) {
printf("%*.*s\n", height + 1, rows + 2, hatch);
}
For the input of 1 your program prints
##
For the input of 5 it prints
##
###
####
#####
######
and so does this. The loop contains a single instruction.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
For some reason, the nested for loop I have created at the bottom seems to be printing out the wrong first value (Gives me 3, when it should be 8). Yet, when I simply do printf (at the bottom), I am given the right value. Not really sure what's wrong with my code.
#include <stdio.h>
int main(void)
{
int d;
printf("Please input dimensions: (between 3 and 9, inclusive): \n");
scanf("%i", &d);
int array[d][d];
int k = 1;
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
array[i][j] = (d * d) - k; //d^2 doesn't work to square a function
k++;
}
}
for (int z = 0; z < d; z++)
{
for (int y = 0; y < d; y++)
{
printf("%i\n", array[z][y]);
}
}
printf("%i\n", array[0][0]);
printf("%i\n", array[0][1]);
}
Edit: Sorry guys, the top value that was being printed was my own input. I was simply thinking it was the first value being printed.
Are you sure you aren't looking at your own input? I cut and paste and see:
Please input dimensions: (between 3 and 9, inclusive):
3
8
7
6
5
...
The 3 is actually what I typed and is echoed.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am working on a lab and I need some help!
Lets say I have a 3 dimensional array double a[2][3][4]. I want to update the values of inside this array through void functions. How would I set that up? I am having trouble initializing a pointer to the double that I want send in as an argument. I want to do something to every value inside of the 3d array so all 24 values?
You probably want this:
void MyFunction (double a[2][3][4])
{
int i,j,k ;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
for (k = 0; k < 4; k++)
{
a[i][j][k] = 5.0 ;
}
}
void main()
{
double a[2][3][4] ;
MyFunction(a) ;
// now every element of array a contains 5.0
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Why does the following code only work on the first iteration around the for loop?
typedef struct {
char name[3];
int gold, silver, bronze, total;
} tally_t;
int main(void)
{
tally_t country[COUNTRIES_COMPETING];
int j;
j=0;
country[j].gold=0;
for (j=0; j<5; j++) {
country[j].gold++;
}
return 0;
}
It's because you only initialized the gold member of the first element of the array. All the rest are uninitialized and have undefined values. Changing an undefined value is undefined behavior.
You just initialized the first element of your array of structure.
In order to fix your problem, you can do it like this :
int main(void)
{
tally_t country[COUNTRIES_COMPETING];
int j;
// Init Step
for (j = 0; j < 5; j++)
country[j].gold = 0:
for (j=0; j<5; j++)
country[j].gold++;
return 0;
}