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");
}
}
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 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 debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
This code aims to exclude non-repeating values in a number list, but does not return repeats in their original sequence. How to keep all repeats with their precedence?
My code:
#include <stdio.h>
#include <stdlib.h>
int count=0;
int main ()
{
int i,j,k,x, altarray[1000],array[1000];
printf("Please enter the number of integers in your list:\n");
scanf("%d",&x);
printf ("Please enter the list of numbers\n");
for (i=0; i<x; i++)
scanf("%d",&array[i]);
printf("\nThe corrected array is...\n");
for (i=0; i<x; i++)
{
for (j=i+1; j<x; j++)
{
if(array[i]==array[j])
{
altarray[count]=array[i];
count++;
}
}
}
for (i=0; i<count; i++)
printf("%d",altarray[i]);
}
To search for duplicated number, you have to cover all elements of your array in the j for loop except the one that have the index i. So that loop should look like this:
for (j = 0; j < x; j++)
{
if ((array[i] == array[j]) && (i != j))
{
altarray[count] = array[i];
count++;
break;
}
}
I have added a break, to avoid wasting cycles after detecting the existence of the duplicated number.
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 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();
}
}
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(" ");
}
}