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;
Related
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.
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
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
It gives an error as terminated due to timeout for large input of t.
To find the number of perfect squares in the given range
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
double s,e,i;
int t;
scanf("%d",&t);
for (;t>0;t--)
{
int cnt=0;
scanf("%lf%lf",&s,&e);
for (i=s;i<=e;i++)
{
if (sqrt(i)==ceil(sqrt(i)))
cnt++;
}
printf("%d\n",cnt);
}
return 0;
}
There is a trick.
Find the square root of lower and upper bound
take their integral part and then
subtract the integral part of lower bound from upper bound.
You also need to check if lower bound is a perfect square or not. If it is then add 1 to the difference.
For example: Number of perfect squares between 1 and 100 is 10 - 1 = 9. Since 1 is also a perfect square therefore add 1 and hence result will be 10.
int result = (int)sqrt(upper_bound) - (int)sqrt(lower_bound);
if(lower_bound == (int)sqrt(lower_bound)*(int)sqrt(lower_bound))
result += 1;
Note that I considered upper and lower bound inclusive.
Method 2 (Efficient) We can simply take square root of a and square root of b and to count the perfect squares between them using
floor(sqrt(b)) - ceil(sqrt(a)) + 1
We take floor of sqrt(b) because we need to consider
numbers before b
We take ceil of sqrt(a) because we need to consider
numbers after a
For example, let b = 24, a = 8. floor(sqrt(b)) = 4
ceil(sqrt(a)) = 3 And number of squares is 4 - 3 + 1 = 2 The two numbers are 9 and 16
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");
}