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 a string array of 10 words and I want to display them.
However, I want the user to press a key and the next values are displayed.
I.e, it should display a, then the user presses a key and it displays b.
How can I achieve this?
char destination[10][10]
for (i=0; i<10; i++){
printf(Enter a name: );
scanf( "%s", name[i])
}
for (i=0; i<10; i++){
printf(" Name %d: %s", i, name[i]);
getch();
}
I'm not sure what you want, nor what your actual problem is, but this works fine here:
#include <stdio.h>
#include <conio.h>
int main() {
char name[10][10];
int i;
for (i = 0; i<10; i++) {
printf("Enter a name : ");
scanf("%s", name[i]);
}
for (i = 0; i<10; i++) {
printf(" Name %d: %s", i, name[i]);
getch();
}
}
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 2 years ago.
Improve this question
So the problem is:" Create a program, that inputs the elements(integers) of a matrix a[n][m] and finds the element with the smallest value amongst the the elements with the biggest value of every column."
So the way I understand it, I have to go through every column and find the biggest number in each, so I will end up with 4 numbers. From these 4 numbers I have to print out the smallest.
int main()
{
int n,m;
int A[n][m];
int B[m];
int max;
int min;
int i,j;
printf("Enter a number for n: ");
scanf("%d",&n);
printf("Enter a number for m: ");
scanf("%d", &m);
for(i=1; i<=n;i++)
{
for(j=1; j<=m;j++)
{
printf("Enter a number [%d][%d]: ", i, j);
scanf("%d", &A[i][j]);
}
}
for(j=1; j<=m; j++)
{
max=A[j][1];
for( i=1; i<=n; i++)
{
if(max<A[j][i])
{
max=A[j][i];
B[j]=max;
}
}
}
min=B[1];
for(i=1; i<=m; i++)
{
if(min>B[i])
{
min=B[i];
printf("%d ", B[i]);
}
}
printf("Min is: %d\n", min);
return 0;
}
My question is where would the error be? It must be a logical one.
I apologize for not asking my question as clearly as most of you in this forum, but it's my first time asking here. Please if you answer, explain it like you would to a beginner.
Thanks!
C programs are generally executed from top to bottom. Therefore you cannot do this:
int n,m;
int A[n][m];
Because n and m has not been given any values. To give them values further down doesn't mean that the line int A[n][m]; will somehow get re-executed.
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 3 years ago.
Improve this question
Whats the meaning of [i] in the following example?
#include <stdio.h>
int main()
{
int values[5];
printf("Enter 5 integers: ");
for (int i = 0; i < 5; ++i)
{
scanf("%d", &values[i]); // HERE
}
printf("Displaying integers: ");
for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}
i here is a variable.
In your code, [i] acts as the index of values and is used to access the element in array values.
Edit:
Since there is a //HERE comment in your code, im going to assume you would also want what [i] does there. The expression &value[i] basically gives the address of value[i] ,i.e, the "ith" element of the array.
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 know that was a lot of questions and answers about "loading matrix from file in C". But I have one problem and question.
My piece of code looks like this:
for(int i=0; fgets(buffer_char, 256, file); i++) {
tmp_char = strtok(buffer_char, " \t");
for(int j=0; tmp_char != NULL; j++) {
printf("%s ", tmp_char);
strtod(tmp_char, array[i][j]);
tmp_char = strtok(NULL, " \t");
}
}
I have input file with matrix like:
3
1 2 3
4 5 6
5 6 7
I'm trying to load it into array, but I have to check if input values are correct. And my question is, How I can do it better? Which solution will be more correct?
#edit:
I modified code to this:
for(int i=0; fgets(buffer_char, 256, file); i++) {
array[i][0] = strtod(buffer_char, &tmp_char);
for(int j=1; tmp_char == NULL; j++) {
array[i][j] = strtod(tmp_char, &tmp_char);
}
}
And it works, How I want, but only for first elements of new line. I know, the problem is with "array[i][j] = strtod(tmp_char, &tmp_char);", but I have no idea what to do.
You can simply use fscanf for validating and storing your input into an array.
Code:
if((check=fscanf(fp,"%d",&n))==1)
{
int Arr[n][n];
for(int i=0; i<n; i++) {
for(int j=0;j<n;j++){
fscanf(fp,"%d",&Arr[i][j]);
printf("%d ",Arr[i][j]);
}
printf("\n");
}
}
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 6 years ago.
Improve this question
I am making the game of fifteen and thus implementing the draw function which prints out the board on the terminal. My function goes as follow:
void draw(void)
{
for(int i=0;i<d;i++)
{
for(int j=0;j<d;j++)
{
if (board[i][j] != 0)
{
printf("%2d ", board[i][j]);
}
else
{
printf(" _ ");
}
}
printf("\n");
}
}
Its working fine for board upto 3X3 but for the board of 4X4 it prints out the following:
In the second column of the second row it prints 1 whereas I expect it to print 10.
You can find out the maximum length of a cell number, and use that to format the printf.
char numstr[20];
int maxlen = sprintf(numstr, "%d", d * d - 1);
...
printf("%*d ", maxlen, board[i][j]);
...
printf("%*c ", maxlen, '_');
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
The number of spaces in each line will change as per the number that is given in the for loop. So if the number changes to 2 as per the for loop, it will have 2 spaces in that line and the number will go on increasing in every line in an ascendinfg order.
int i, j;
for(i = 0; i < 10; i++){
for(j = 0; j < i; j++) printf(" ");
printf("\n");
}
int i,j;
for (i=1; i<5; i++){
for (j=1; j<=i; j++){ // it will print i spaces whenever i increases.
printf(" ");
}
}