Error in C code while using 2d array - c

I am trying to implement this simple code(no algo as such).
#include<stdio.h>
#include<stdlib.h>
int main()
{
int l,h1,h2,h3,i,j,t,m,n;
scanf("%d %d",&m,&n);
int A[m][n];
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
scanf("%d",&A[i][j]);
}
int n1=m*n;
int adj[n1][n1];
printf("Before initialization array A =\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
printf("%d ",A[i][j]);
printf("\n");
}
for(i=1;i<=n1;i++)
{
for(j=1;j<=n1;j++)
adj[i][j]=-1;
}
printf("After initialization array A =\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
printf("%d ",A[i][j]);
printf("\n");
}
return 0;
}
This is my input file -
4 3
2 3 2
2 5 1
5 3 1
3 1 1
So here 1st 2 elements are m,n i.e. no of rows and columns.Then for m rows there are n elements.I store these elements in a 2d array A. I then print this array.It gives the correct answer.Then I am making new arrary adj[m*n][m*n].I initialize this array to -1.After that when I print my A array back first five element of A also becomes -1.I am not changing value of A.So why is this happening.This is the output I get-
Before initialization array A =
2 3 2
2 5 1
5 3 1
3 1 1
After initialization array A =
-1 -1 -1
-1 -1 1
5 3 1
3 1 1

C uses 0 based indexing. So, the valid indices of an array start from 0 and end at length-1.
This means that you'll have to modify your loops. This:
for(i=1;i<=m;i++)
and needs to be
for(i=0;i<m;i++)
You'll need to do the same for the all the other loops. Otherwise, you access an invalid array index and this leads to Undefined Behavior.

What you need to know is that in C/C++ array starts from 0 to array length -1. Change this in for loops and see what happens

Related

program not outputting the correct smallest most frequently appeared number

I was making a program in C Language that determines the most frequently appeared number in an array, and also outputting the smallest most frequently appeared number, however I am encountering a bug and I was not sure which part I did wrong.
Here is the example of the program input
2
8
1 1 2 2 3 4 5 5
8
5 5 4 3 2 2 1 1
The number 2 means to create 2 different arrays, the number 8 determines the size of the array. the number afterwards determine the numbers to be put inside of the array, and the program repeats itself by inputting the size of an array etc.
Here is the expected output :
Case #1: 2
1
Case #2: 2
1
The "Case #1: 2" means that the most frequently appeared number appeared 2 times in the array (number 1 2 and 5, each appeared twice in the array), while it prints number 1 because number 1 is the smallest number in the most frequently appeared. and the same goes on for case #2
However, in my program, when I input the second case, it does not properly print the smallest number, and instead just prints nothing. But weirdly enough, if I input a different number in the second array (instead of just the first array in reverse order) it prints the correct smallest number. Here is the example output that my program creates
Case #1: 2
1
Case #2: 2
Here is the code that I made :
#include <stdio.h>
int main(){
long long t, n;
scanf("%lld",&t); //masukkin berapa kali mau bikin array
for(int x=0;x<t;x++) {
scanf("%lld",&n); // masukkin mau berapa angka per array
long long arr[200000]={0};
long long i, count, freq=0, test=0;
for(i=0; i<n; i++){
scanf("%lld", &count); //masukkin angka ke dalem array
arr[count]++;
}
for(i=0; i<200000; i++){
if(arr[i]>0 && arr[i]>=freq){
freq=arr[i];
}
}
printf("Case #%d: %lld\n",x+1,freq);
int min;
for(i=0; i<200000; i++){
if (arr[i] > min){
min = arr[i];
printf("%lld",i);
test=1;
}
}
printf("\n");
}
return 0;
}
Your problem is here:
int min;
min is not initialized so when you do if (arr[i] > min){ you have no idea what value min has.
So do
int min = INT_MIN;
That said, you don't really need min. The first arr[i] that equals freq will tell you that i is the smallest number.
Further notice that
long long arr[200000]={0};
is a bad idea. Huge arrays should never be defined as local variables as it may lead to stack overflow. Make it a global variable or use dynamic allocation.
And you should change
arr[count]++;
to
if (count >= 200000)
{
// Too big - add error handling
...
}
else
{
arr[count]++;
}
And you don't need an extra loop for finding freq. Find freq when you get the input.

I tried make a bubble sort with C but it's not working (It's doing bubble sort 10 times)

first i giving how many numbers will i enter then i enter numbers. he need make bubble sort to them but its writing like======>
1 3 2 6 = 0 0 0 0(but it must be like 1 2 3 6(small to big))
the app how i want=
7
1 5 2 7 4 7 3
1 2 3 4 5 7 7
#include<stdio.h>
int main()
{
int numbers[500000];
int counter=0;
int howmany;
scanf("%d",&howmany);//getting how many numbers will we enter
int howmany2=howmany;//we will use this for write all numbers after all
while(howmany>0)//getting all numbers
{
scanf("%d",&numbers[counter]);
howmany--;
counter++;
}
int checker1,checker2;//its gonna check first and second number, then second and third...etc
int hm=howmany-1;//its gonna check entered number-1 times(1,2,3)={1,2},{2,3}
int clone1;//later we will copy numbers[checker1]
int tentime=10;//we gonna do bubble sort 10 times
while(tentime>0)//doing bubble sort
{
checker1=0;
checker2=1;
while(hm>0)
{
if(numbers[checker1]>numbers[checker2])
{
clone1=numbers[checker1];
numbers[checker1]=numbers[checker2];
numbers[checker2]=clone1;
}
checker1++;
checker2++;
hm--;
}
tentime--;
}
int counter2=0;
while(howmany2>0)//showing new number sort on screen
{
printf("%d ",numbers[counter]);
howmany2--;
counter2++;
}
printf("\n");
return 0;
}
You have several problems in your code:
At the end of your first while loop howmany will be 0. As a result hm will be set to -1 and the sort loop (while hm > 0 ) will never run.
When you print out the results are using counter as the array index (this is 4 which is out of bounds and never changes since you are incrementing counter2. As a result you are printing out an undefined value (0 in your case) four times.
Declaring an array of size 500000 may blow up your stack. Even if not it is way larger than you need

Multidimensional array: printing addition table in C

int main(){
int row=0,col=0;
printf("Enter number of rows of the table\n");
scanf("%d",&row);
printf("Enter number of columns of the table\n");
scanf("%d",&col);
printTable(row,col);
}
void printTable(int row,int col){
int i =0,j=0,k=1,L=1,num=0;
printf("row: %d, col: %d\n",row,col);
int table[row][col];
for (i;i<row;i++){
for (j;j<col;j++){
table[i][j] = i+j;
printf("%d ", table[i][j]);
}
printf("\n");
}
}
Trying to print an addition table using multidimensional array, my output looks like this:
row: 4, col: 4
0 1 2 3
But I'm supposed to get
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
Please help me get right output. thanks
for (j;j<col;j++){
You aren't resetting j to zero before the second and subsequent passes. Change that to
for (j=0;j<col;j++){
You might want to change the other for-loop (on i) as well. Generally, unless you have a Very Good Reason for not doing so, you should always be initializing the loop variable before the first pass; that's why that first clause of the for syntax exists, after all.
By the way, if you didn't want to initialize the loop variable, you could have written this as for (;j<col;j++){ -- as you wrote it, the j is conceptually just retrieving that variable's value and throwing it away, and the for permits just leaving the initization, test, and/or update clauses empty if you don't need them. In fact, for(;;) { means the same thing as while(true) {, loop forever... and some people like to set up a macro,
#define EVER ;;
just so they can write the cutesy for(EVER) {
If you want output like
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
Then following code prints the same outpit
for (i=0;i<row;i++){
for (j=0;j<col;j++){
table[i][j] = i+j;
printf("%d ", table[i][j]);
}
printf("\n");
}

Populate a 2D Array by a rule

How do I populate an array like this:
1 2 3 4
2 3 4 3
3 4 3 2
4 3 2 1
I need to find out a formula that determines the pattern of populating this array.
#include <stdio.h>
#define N 4
int main()
{
int i,j,arr[N][N];
int a=1;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
arr[j][i]=i+j+1;
}
for(i=0; i < N; i++)
for(j=0;j<N;j++)
printf("%2i ",arr[j][i]);
printf("\n");
return 0;
}
It prints out similarly to the desired array except that I need "3" at the end of the second row, and after that point, it goes reversed. Please, explain me how to do that.
Try this:
arr[j][i]=N-abs(i+j-(N-1));
abs() can be used any time you need a numerical sequence which is mirrored around some value. You just need to subtract a constant such that the value you want to mirror around is zero, take the absolute, and then re-adjust the output.
In your case (with N = 4) the (i+j) summation produces: 0,1,2,3,4,5,6. The middle value is N-1, as the largest value is 2 * (N-1).
Subtracting N-1 (3) gives: -3,-2,-1,0,1,2,3.
The abs() gives: 3,2,1,0,1,2,3.
If we subtract that from N (4) we get the desired 1,2,3,4,3,2,1 sequence.
arr[j][i]=(j * 3 + j/ 3 + b) % N + 1;

How could I circularly shift each row of an 2-d array by two positions?

First of all I want to show you what actually I want........
if input is ...
2 3 4
5 6 6
7 5 4
the output should be ...
7 5 4
2 3 4
5 6 6 /*Each row is shifted circularly left by two positons */
I tried this code acc. to my knowledge (I am a beginner in C) and have written this thing ..
/*To shift row of a 4 * 5 matrix by 2 positons left*/
#include<stdio.h>
int main() {
int a[4][5],i,j,k,(*temp)[5];
for(i=0;i<=3;i++) {
for(j=0;j<=4;j++)
scanf("%d",*(a+i)+j);
}
for (k=1;k<=2;k++) {
for(i=0;i<=3;i++) {
temp = (a+i); /*I thought that *(a+i) will point to the address of each row and so I should take it in a variable which is capable of pointing to a row of 5 variables that why TEMP */
(a+i) = (a+i+1);
(a+i+1) = temp;
}
}
for(i=0;i<=3;i++) {
for(j=0;j<=4;j++)
printf("%d\t",*(*(a+i)+j));
printf("\n");
}
return 0;
}
where am I wrong.....Please correct me ????
Your sample output looking like shifted along column :)
scanf("%d",*(a+i)+j); is not a good way, use
scanf("%d",&a[i][j]); instead
You tried to copy a row at temp = *(a+i);, but you can only
copy adresses here. temp is going to point a[i], but won't copy
it's data.
This code below gives
input
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
output
3 3 3 3 3
4 4 4 4 4
1 1 1 1 1
2 2 2 2 2
I have shifted columns like your sample and used a new array b instead of temp
#include<stdio.h>
int main() {
int a[4][5],i,j,b[4][5];
for(i=0;i<=3;i++) {
for(j=0;j<=4;j++)
scanf("%d",(*(a+i)+j));
}
for(i=0;i<=3;i++)
for(j=0;j<=4;j++)
{
*(*(b+i)+j)=*(*(a+((i-2+4)%4))+j);
}
for(i=0;i<=3;i++) {
for(j=0;j<=4;j++)
printf("%d\t",*(*(b+i)+j));
printf("\n");
}
return 0;
}
Although the concept that you are driving at could be made to work (but there is a LOT wrong with it at the moment) using pointer arithmetic in this context makes the code look very complicated so I wonder why you don't try to rewrite this using array syntax.
For example you could write your output like this:
for(i=0;i<=3;i++)
{
for(j=0;j<=4;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
I think this is the easier syntax for a beginner to understand. Similarly the row cycle / swap is far more transparent in this form.

Resources