Pointer pointing older address while printing the array elements through loop - c

I want to read array elements defined in main function through another user defined function. The array is 2D and it shows the first three elements correctly but as next loop starts the address pointed by that pointer is 2 step back from the intended address. Why's that?
Here is the main function calling the frame() function where the problem is:
void main(){
char dec,player[2][20];
int i,counter=0,palo,winner=0;
for(i=0;i<2;i++){
printf("Enter Player%d's name: ",(i+1));
scanf("%s",player[i]); //ASK PLAYER NAME
}
startAgain: //GAME RESTART POINT
system("cls");
palo=0;
char spot[][3]={"123","456","789"};
//------------------MAIN GAME AREA-------------------------------
for(counter=0;counter<9;counter++,palo++){
frame(*spot);
read(&palo,*spot,*player);
palo %=2;
}
}
Here is the frame() function:
void frame(char *count){
int i,j;
printf("\t\t\t");
line(24);
for (i = 0; i < 3; i++){
printf("\t\t\t");
for (j = 0; j < 3; j++){
printf("| %c ",(*(count+i)+j));
}
printf("|\n\t\t\t");
line(24);
}
}
The intended output is :
1 2 3
4 5 6
7 8 9
What it displays:
1 2 3
2 3 4
3 4 5

Make life easier for yourself and others, use normal array indexing instead of pointer arithmetic.
for(counter=0;counter<9;counter++,palo++){
frame(spot);
read(&palo,spot,player);
palo %=2;
}
...
void frame(char count[][3]){
int i,j;
printf("\t\t\t");
line(24);
for (i = 0; i < 3; i++){
printf("\t\t\t");
for (j = 0; j < 3; j++){
printf("| %c ",count[i][j]);
}
printf("|\n\t\t\t");
line(24);
}
}

Related

Copy an array to another while sorting it simultaneously

I have already done the sorting part and the copy but I am really struggling to put it together.
Here is only the sorting part because i am not so familiar with it. My main problem is when I start copying the array it only copies the first 2 number then stops and I think its a small problem in the for loop somewhere but i cant find it.
int main ()
{
int number[30];
int number2[30];
int i, j, a, n;
printf("Enter the value of N\n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] < number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The array is copied and sorted like:\n");
for (i = 0; i < n; ++i)
{
printf("%d\n", number[i]);
}
system("Pause");
}
I compiled (MinGW) and ran this exact code and got the following output:
Enter the value of N
10
Enter the numbers
9
1
0
2
8
3
7
5
4
6
The array is copied and sorted like:
9
8
7
6
5
4
3
2
1
0
Press any key to continue . . .
It looks like it is correctly reverse-sorting the input values. Note that this is a fairly common sorting algorithm (https://www.geeksforgeeks.org/bubble-sort/).
Based on this I would argue that the for loop works as is, assuming that you are looking for a reverse sort.
I am not sure what exactly is meant by "copying the array". Array number2 is unused and there are no operations that appear to copy the array number, just operations for loading it, sorting it and printing it.

output is not in proper manner for 2d array in c

two dimentional array in c
im getting small error which screwing me up..
can u spot any error, while i should get op as
1 2 3
4 5 6
7 8 9
but am getting op as
1 2 4
4 5 7
7 8 9
#include <stdio.h>
int main(int argc,char* argv[])
{
int m;
scanf("%d",&m);
int a[m][m],i,j;
for(i=0;i<=m;i++){
for(j=0;j<=m;j++){
scanf("%d",&(a[i][j]));
printf("%d",a[i][j]);
}
}
for(i=0;i<=m;i++){
for(j=0;j<=m;j++){
printf("%d",a[i][j]);
printf("\t");
}
printf("\n");
}
}
The declaration :
int a[m][m];
means that you have an array with m rows and m columns, numbered from 0 to m-1. You are trying to access elements which do not belong to your array, due to your <=m conditions.
Change both of your loops from :
for(i=0;i<=m;i++){
for(j=0;j<=m;j++){
scanf("%d",&(a[i][j]));
printf("%d",a[i][j]);
}
}
which you have now, to :
for(i = 0; i < m; i++){
for(j = 0; j < m; j++){
scanf("%d",&(a[i][j]));
printf("%d",a[i][j]);
}
}
You can read more about indexing and arrays here.
Your loops are going beyond the end of your array. An array like int a[m] has elements going from 0 to m-1 so your loops should be like this:
for(i=0;i<m;i++){
for(j=0;j<m;j++){

Transfering a 2-dimensional array into a single dimension array

I'm trying to make a program in C that transfers a 2-dimensions-array(a matrix to be particular) into a single-dimension-array. For example, if we have a matrix with L lines and C columns, it should turn into a a single line newL=L*C. Therefore, if the matrix has 3 lines and 4 columns, the new array will have 3*4=12 as its size.
The matrix should turn to this:
1 2
--> 1 2 3 4
3 4
The problem I'm facing right now, is how to assign the matrix to the array without having random values or repeated values.
The piece of code I'm concerned with, goes like this:
for(k=1;k<=t;k++)
{
for(i=1;i<=l;i++)
{
for(j=1;j<=c;j++)
{
v[k]=m[i][j];
}
}
}
k,i and j are counters of the matrix(2-dimensions-array) and the the array. two of which; i and j, are counters for the matrix and k is the array's counter. Notice that each one of them starts from 1 and goes to its size and in this size I will use 2 lines and 2 columns for the matrix therefore the array will have a size of 4(2*2).
l is the number of lines in the array.
c is the number of colunms in the array.
t is the size of the array. t=l*c
Executing the code gives me this as a return:
1 2
--> 4 4 4 4
3 4
Simply said, the piece of code will ALWAYS give the last value of the matrix to the array. So if I replace 4 with 5 in the matrix, the array will have 5 5 5 5.
EDIT:
Here is the full code to understand what I'm trying to do:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c,i,j,l,k,t;
printf("Donner le nombres des lignes: ");
scanf("%d",&l);
printf("Donner le nombres des colonnes: ");
scanf("%d",&c);
int m[l][c];
t=l*c;
int v[t];
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
printf("Donner m[%d][%d]: ",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",m[i][j]);
}
printf("\n");
}
printf("\n\n\n\n");
for(k=1;k<=t;k++)
{
for(i=1;i<=l;i++)
{
for(j=1;j<=c;j++)
{
v[k]=m[i][j];
}
}
}
for(k=0;k<t;k++)
{
printf("%d\t",v[k]);
}
system("pause");
}
Thank you guys for the help, I found the correct way to do it.
You need not the outer loop
Array indices are zero-based in C
Thus, we have:
for(k = 0, i = 0; i < o; i++)
{
for(j = 0; j < p; j++)
{
v[k++] = m[i][j];
}
}
where o and p - dimensions of the matrix m
If we have a multidimensional array like this:
int nums[3][3];
And we have:
int all[9];
And we've got:
int a, b;
We'll reference each of the nums like this:
nums[a][b];
Now think of what the values of a and b will actually be:
for (a = 0; a < 3; a++) {
for (b = 0; b < 3; b++)
all[((a * 3) + b)] = nums[a][b];
}
This will work so long as you multiply a with the number of elements it will iterate:
int nums[5][5];
int all[25];
int a, b;
for (a = 0; a < 5; a++) {
for (b = 0; b < 5; b++)
all[((a * 5) + b)] = nums[a][b];
}
You mention your question is "how to I fix the code?" I think plenty of people have given you the correct answer. This is your code along with the corrected code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c,i,j,l,k,t;
printf("Donner le nombres des lignes: ");
scanf("%d",&l);
printf("Donner le nombres des colonnes: ");
scanf("%d",&c);
int m[l][c];
t=l*c;
int v[t];
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
printf("Donner m[%d][%d]: ",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",m[i][j]);
}
printf("\n");
}
printf("\n\n\n\n");
/* corrected code below */
k = 0;
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
v[k]=m[i][j];
k++;
}
}
/* corrected code above */
for(k=0;k<t;k++)
{
printf("%d\t",v[k]);
}
system("pause");
}
As long as the new array is the correct size, something like the following should work:
k=0;
for(i=0;i<l;i++){
for(j=0;j<c;j++){
v[k]=m[i][j];
k++;
}
}
Essentially, you are traversing over the matrix (your lines and columns--as you put it) and at the same time increasing the position (k) in the new array where you want that value to be put.
This:
for(k=1;k<=t;k++)
for(i=1;i<=l;i++)
for(j=1;j<=c;j++)
v[k]=m[i][j];
does not do what you think. Think about when you first loop through the j part, you will be setting all the 0th element of v the entire time, finally the last value you set will stick (ie, the one in position 1, 1 which happens to be 4). Then you will increment k to 1 and repeat it again, resulting in all 4's. You want this:
for(i = 0; i < l; i++)
for(j = 0; j < c; j++)
v[i*l+j] = m[i][j]; // i*l + j gives you the equivelent position in a 1D vector.
Make sure your v vector is the right size ie. int v[l*c];. Also remember that in c zero indexing is used.If you really do need 1 based indexing (which you dont ...) then do this:
int k = 1;
for(i = 1; i <= l; i++)
for(j = 1; j <= c; j++)
v[k++]=m[i][j];
But remember that this will make any further operations on this vector Gross. So dont do this ....
If you just want to access the matrix elements as a single dimension array, you could declare an int pointer v:
int m[3][4];
int *v = (int*)m;
// then access for example m[1][1] as v[5]
Or, to actually copy the array, use a double for (as in the other answers), a single for like below
int vv[12];
for(i = 0; i < 12; i++)
vv[i] = m[i/4][i%4];
or just use memcpy:
memcpy(vv, m, 12*sizeof(int));

How to print from 1 to 9 with three numbers in a line using loops

I am coding a tic tac toe game in C. I am stuck at making a board like this:
1 2 3
4 5 6
7 8 9
I want to use loops so that I dont have to use a printf function with many \n's and \t's...
Here's my attempt:
for (i=0;i<=9;i++)
{
printf("\n\n\n\t\t\t");
for (j=i;j<=i+2;j++)
{
printf("%c\t",boarddots[j]);
}
if (i==3)
break;
}
Something like this, you could adapt it to your actual needs:
for(int i = 1; i <= 9; ++i)
{
printf("%d", i); // print numbers one by one
if (0 == i % 3)
printf("\n"); // start new line if current number is divisible by 3
}
P.S. Sorry for possible typos
for (int row = 0; row < 3; row++)
{
for (int column = 0; column < 3; column++)
{
printf("%d ", (row * 3) + column + 1);
}
printf ("\n");
}
/*
output:
1 2 3
4 5 6
7 8 9
*/
Your loop condition for (i=0;i<=9;i++) iterated once too many. Personally, I would uses a 2D array such as char board [3][3], but one step at a time to help with your immediate question.
#include<stdio.h>
char boarddots[] = "--O-XX-O-";
int main()
{
int i;
for (i=0; i<9; i++) {
if (i % 3 == 0)
printf("\n\n\n\t\t");
printf("\t%c",boarddots[i]);
}
return 0;
}
Could do print as a string and use string truncation:
char boarddots[9] = {'1','2','3','4','5','6','7','8','9'};
int loop;
for (loop=0; loop<9; loop+=3)
printf ("%.3s\n", &boarddots[loop]);
You don't need a NULL on the end of the char array as the truncation takes care of that.

fscanf read file using c

My data.txt content is:
1 2 3 4 5 6
1 2 3 4 5 6
4 5 6 7 8 2
I read the file, and store the value to a two dimension int array
int record[line_number][6];
int record2[line_number][8];
int test;
for(i = 0; i <line_number; i++)
{
for(j = 0; j <6; j++)
{
fscanf(fptr, "%d", &record[i][j]);
}
}
int a=0;
int b=0;
for(a=0; a<i; a++) {
for(b=0; b<6; b++) {
printf("%d,", record[a][b]);
}
printf("\n");
}
The output like a memory address, what wrong in my code? Thanks!
You don't check the return value of fscanf(), so you don't know that it really succeeds for all the conversions. If it fails, the value in record[][] will be uninitialized, and printing it out will print whatever happens to be in memory.

Resources