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++){
Related
I have written this code in C that implements the counting sort algorithm. I create an array of 10 elements and then I apply the counting sort. The code is this:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
int my_array[] = {10,10,9,9,6,5,4,3,2,1};
int i;
int N = 10;
//print the array
for (i=0; i<10; i++){
printf("%d\n", my_array[i]);
}
//define the minimum and the maximum as the first element of the array
int min_array=my_array[0];
int max_array=my_array[0];
printf("--------------\n");
//find the minimum and the maximum of the array
for(i=0; i<N; i++){
if(my_array[i]<min_array){
min_array=my_array[i];
}
else if(my_array[i]>max_array){
max_array=my_array[i];
}
}
//check if it worked
printf("max_array %d\n", max_array);
printf("min_array %d\n", min_array);
//
int range_array;
range_array = max_array - min_array +1;
int count_array[range_array+1];
for(i=0; i<range_array;i++)
count_array[i]=0;
int j=0;
for(int i=0; i<10; i++){
count_array[my_array[i]-min_array]=count_array[my_array[i]-min_array]+1;
}
int z = 0;
for(i=min_array; i<max_array; i++){
for(j=0; j<count_array[i-min_array];j++)
z=z+1;
my_array[z]=i;
}
for(i=0; i<N; i++){
printf("%d\n", my_array[i]);
}
}
When I execute it, I have Segmentation fault: 11
I do not know why, but I think it is related to some allocation problem, and in particular I think that the error comes from one of the last for loops before printing the ordered array. Indeed if I put some print check where it stops, the segmentation fault error comes at the end.
EDIT: I fixed count_array[range_array] into count_array[i] in the for loop. But now it seems not working properly:
The output is this:
10 10 9 9 6 5 4 3 2 1
--------------
max_array 10
min_array 1
--------------
1 2 3 4 5 6 9 9 2 1
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.
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);
}
}
This is my code:
//bubble sort
#include<stdio.h>
int main(){
int a[11];
int temp;
int i,j;
//input
for(i=1;i<=10;i++){
scanf("%d",&a[i]);
}
//sort
for(i=1;i<10;i++){ //the number of number
for(j=1;j<10-i;j++) //--
if(a[j]<a[j+i]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
//output
for(i=1;i<=10;i++){
printf("%d ",a[i]);
}
getchar();
getchar();
return 0;
}
The result was not what I was expecting. The input that I have used is 1 2 3 4 5 6 7 8 9 0 but after the sort the output is 8 6 5 4 3 7 2 9 1 0.
Two things:
A typo (I believe):
if(a[j]<a[j+i]){
should have been
if(a[j]<a[j+1]){
Secondly,
for(j=1;j<10-i;j++)
should have been
for(j=1;j<10-i+1;j++)
You need to run the loop one extra time, to accommodate all swaps.
Ideone link
Note that arrays are zero based - you should start i = 0 in all instances - it seems you only need 10 numbers, so each iteration needs to be from 0..9, and array of int a[10]; will suffice.
Assuming you want the bubblesort output ordered from lowest to highest, the algorithm should be:
for(i=0; i < 10; i++){
for(j=0; j < 10 - i - 1; j++) // Because we are looking ahead one cell, -1
if(a[j] > a[j+1]){ // next cell
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
Meaning, after the first pass, the highest number will be in a[9]. The next loop will set the next highest in a[8]. etc.
Trying to make 3 x 3 matrix multiplier but it gives out wrong output. I don't know what I am doing wrong. Two problems that I am facing are:
(1) Some variables store wrong input. For example a[1][1] shows 7 although I entered 1
(2) The matrix multiplication is wrong
#include <stdio.h>
#include <conio.h>
void matrix_format(int m[2][2])
{
int i,j;
printf("\n\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(j==0)
printf("[ %d |",m[i][j]);
else if(j==1)
printf(" %d |",m[i][j]);
else if(j==2)
printf(" %d ] \n",m[i][j]);
}
}
}
int main(void)
{
void matrix_format(int [2][2]);
int a[2][2], b[2][2], r[2][2],m,i,j;
clrscr();
for(m=1;m<=2;m++)
{
if(m==1)
{
printf("Enter values for the matrix A \n");
}
else
{
printf("\n\nEnter values for the matrix B \n");
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(m==1)
{
printf("A[%d][%d] : ",i+1,j+1);
scanf("%d",&a[i][j]);
}
else if(m==2)
{
printf("B[%d][%d] : ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
}
}
printf("\n Matrix A : \n");
matrix_format(a);
printf("\n Matrix B : \n");
matrix_format(b);
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
r[i][j]= a[i][j] * b[j][i];
}
}
printf("\n Matrix Multiplication Result : \n");
matrix_format(r);
getch();
return 0;
}
output:
Please guide me.
The first problem that jumps out is that all your arrays are 2x2, while they should be 3x3:
m[2][2]
should read
m[3][3]
and so on. The number in brackets is the size of the array, not the index of the last element.
This will explain some of the weirdness, in particular why some elements get mysteriously overwritten.
As to the actual matrix multiplication, your algorithm isn't quite right (assuming what you're trying to implement is the standard linear algebra matrix product). Think about what steps are involved in multiplying two matrices, and what your code is actually doing. Since this is homework, I'll only give you a hint:
Matrix product involves summations of element products.
There are two major problems:
First, a 3*3 matrix is represented by int matrix[3][3] not int matrix[2][2]. The reason you see strange results is that you are writing over array boundaries, effectively writing over the other matrix because their memory locations are adjacent.
Note: An array such as int a[10] can only be indexed from 0 to 9.
Another problem is your multiplication. From math, we know that if we have:
C = A x B
Then we have:
C[i][j] = sum(A[i][k]*A[k][j]) over k
That is in your case:
C[i][j] = A[i][0]*A[0][j]+A[i][1]*A[1][j]+A[i][2]*A[2][j]
So you have to have:
for over i
for over j
C[i][j] = 0
for over k
C[i][j] += A[i][k]*B[k][j]
I have written a simple matrix multiplication program without using pointers. Hopefully this would work for you. I can see that you know how to use functions, so try using them more often. Also your multiplication logic was wrong. Read up on that and then see the code. (If you want to do the matrix multiplication for let's say a 5 x 5 matrix, then you should just change #define SIZE 3 to #define SIZE 5).
#include <stdio.h>
#include <stdlib.h>
#define SIZE 3
void CreateMatrix(char name, int m[SIZE][SIZE]) {
int row, col;
printf("Enter values for the matrix %c:\n", name);
for(row = 0; row < SIZE; row++) {
for(col = 0; col < SIZE; col++) {
printf("%c[%d][%d] : ", name, row + 1, col + 1);
scanf("%d", &m[row][col]);
}
}
printf("\n");
}
void PrintMatrix(char name, int m[SIZE][SIZE]) {
int row, col;
printf("Matrix %c:\n", name);
for (row = 0; row < SIZE; row++) {
printf("[ ");
for (col = 0; col < SIZE; col++) {
printf("%d ", m[row][col]);
}
printf("]\n");
}
printf("\n");
}
void MatrixMultiply(int a[SIZE][SIZE], int b[SIZE][SIZE], int mul[SIZE][SIZE]) {
int row, col, k;
for (row = 0; row < SIZE; row++) {
for (col = 0; col < SIZE; col++) {
mul[row][col] = 0;
for (k = 0; k < SIZE; k++) {
mul[row][col] += a[row][k] * b[k][col];
}
}
}
}
int main() {
int a[SIZE][SIZE];
int b[SIZE][SIZE];
int mul[SIZE][SIZE];
// Create Matrices
CreateMatrix('A', a);
CreateMatrix('B', b);
// Matrix Multiplication
MatrixMultiply(a, b, mul);
// Print Matrices
PrintMatrix('A', a);
PrintMatrix('B', b);
PrintMatrix('M', mul);
}
The output:
Enter values for the matrix A:
A[1][1] : 1
A[1][2] : 2
A[1][3] : 3
A[2][1] : 4
A[2][2] : 5
A[2][3] : 6
A[3][1] : 7
A[3][2] : 8
A[3][3] : 9
Enter values for the matrix B:
B[1][1] : 1
B[1][2] : 2
B[1][3] : 3
B[2][1] : 4
B[2][2] : 5
B[2][3] : 6
B[3][1] : 7
B[3][2] : 8
B[3][3] : 9
Matrix A:
[ 1 2 3 ]
[ 4 5 6 ]
[ 7 8 9 ]
Matrix B:
[ 1 2 3 ]
[ 4 5 6 ]
[ 7 8 9 ]
Matrix M:
[ 30 36 42 ]
[ 66 81 96 ]
[ 102 126 150 ]
First, see #aix' answer regarding the array sizes. Then, the reason the multiplication doesn't work is that you are using the wrong formula. The element at i,j in the result matrix is not simply the product of i,j and j,i from the two matrices being multiplied - instead, every element in row i from the left matrix must be multiplied by the corresponding element from column j from the right matrix, and all the products must be added together. See this illustration in the Wikipedia article.
you have define array of 2*2 i.e. it has index of 0,1.but in your FOR loop you are trying to accept 3 i.e.{0,1,2 }elements in one row. so remove = sign from all FOR loops. or just change the declaration of your Array to [3][3].Also then apply right formula for Matrix multiplication i.e r[0][0]=(a[0][0]*b[0][0])+(a[0][1]*b[1][0])+(a[0][2]*b[2][0]). for first cell so on for other cells,in your case for 3*3 matrix.