I'm trying to create a program in C that copies numbers from one 2d array to another, but specifically. (see image)
Code:
int main(){
int numbers[6][8] = {
{1,1,1,1,1,1,1,1},//0
{2,2,2,2,2,2,2,2},//1
{3,3,3,3,3,3,3,3},//2
{4,4,4,4,4,4,4,4},//3
{5,5,5,5,5,5,5,5},//4
{6,6,6,6,6,6,6,6} //5
};
int output[2*8][3];
for(int i = 0; i < 3; i++){
for(int j = 0; j < 2*8; j++){
output[j][i] = numbers[i][j];
}
}
for(int j = 0; j < 2*8; j++){
for(int i = 0; i < 3; i++){
printf("%d ", output[j][i]);
}
printf("\n");
}
}
My output:
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
2 3 4
2 3 4
2 3 4
2 3 4
2 3 4
2 3 4
2 3 4
2 3 4
But it's bad.
The output should look like this:
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
4 5 6
4 5 6
4 5 6
4 5 6
4 5 6
4 5 6
4 5 6
4 5 6
Problem is that in my output in second part I have number from rows 1, 2 and 3 (from numbers array). But it was supposed to be the 3, 4 and 5 row.
You have to get your indexes right :-)
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 16; j++) {
output[j][i] = numbers[j / 8 * 3 + i][j % 8];
}
}
In base of your code, i split in two cases, and the second part i put +2 like this: output[j][i] = numbers[i+2][j];
#include <iostream>
using namespace std;
int main(){
int numbers[6][8] = {
{1,1,1,1,1,1,1,1},//0
{2,2,2,2,2,2,2,2},//1
{3,3,3,3,3,3,3,3},//2
{4,4,4,4,4,4,4,4},//3
{5,5,5,5,5,5,5,5},//4
{6,6,6,6,6,6,6,6} //5
};
int output[2*8][3];
for(int i = 0; i < 3; i++){
for(int j = 0; j < 8; j++){
output[j][i] = numbers[i][j];
}
}
for(int i = 0; i < 3; i++){
for(int j = 8; j < 16; j++){
output[j][i] = numbers[i+2][j];
}
}
for(int j = 0; j < 2*8; j++){
for(int i = 0; i < 3; i++){
printf("%d ", output[j][i]);
}
printf("\n");
}
}
Related
Here is my code:
#include <stdio.h>
#include <stdlib.h>
int main() {
system("cls");
int count = 0;
int arr[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
for (int k = 0; k < 8; k++) {
if (arr[i] + arr[j] + arr[k] == 12 && arr[i] != arr[j] && arr[j] != arr[k]) {
count = count + 1;
}
}
}
}
printf("Count=%d", count);
return 0;
}
My expected output is 6. But I'm getting 36 as output.
When I change j = i + 1, and k = j + 1, the code works. But I'm not sure why it works. Mine code should also work as I've tested all possible conditions.
I understand why my code doesn't work it's because it produces below output:
1 3 8
1 4 7
1 5 6
1 6 5
1 7 4
1 8 3
2 3 7
2 4 6
2 6 4
2 7 3
3 1 8
3 2 7
3 4 5
3 5 4
3 7 2
3 8 1
4 1 7
4 2 6
4 3 5
4 5 3
4 6 2
4 7 1
5 1 6
5 3 4
5 4 3
5 6 1
6 1 5
6 2 4
6 4 2
6 5 1
7 1 4
7 2 3
7 3 2
7 4 1
8 1 3
8 3 1
At the end of the long if statement you have
arr[i] != arr[j] != arr[k]
I guess you are trying to compare all 3 variables but that's not what the code does. Besides that you have already made that check with arr[i] != arr[j] && arr[j] != arr[k] && arr[i] != arr[k] so you don't need it.
That said, I find it strange that you check the values for being equal. I would expect that you should check the index, i.e. like i != j && i != k && j != k.
Anyway your code is also wrong because you start all three loops from zero. Starting all loops from zero causes the code to check the same triplet several times, i.e.
i j k
-------
1 2 3 will be checked
1 3 2 will be checked
2 1 3 will be checked
2 3 1 will be checked
3 1 2 will be checked
3 2 1 will be checked
So you check the same triplet 6 times. That's not what you want (I assume).
Instead consider:
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("cls");
int count = 0;
int arr[8] = {1, 2, 3, 4, 5, 6, 7, 8};
for (int i = 0; i < 8; i++)
{
for (int j = i+1; j < 8; j++)
{
for (int k = j+1; k < 8; k++)
{
if (arr[i] + arr[j] + arr[k] == 12)
{
count = count + 1;
}
}
}
}
printf("Count=%d", count);
return 0;
}
I am a beginner in programming, I studied all about C, I started to solve problems from hackerrank.com, there I faced a problem to print a pattern like given below
(the output of problem program):
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4
the input will be an integer which will provide the data for the length of pattern square, here it is 4 in image,
I tried a lot to type a proper logic and I end up with this useless code bellow:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
int array[n- 1 + n][n - 1 + n];
array[(n - 1 + n) / 2][(n - 1 + n) / 2] = 1;
int f[n];
for(int i = 0; i < n; i++) {
f[i] = i;
}
for(int i = 0; i < n - 1 + n; i++) {
for(int j = 0; j < n - 1 + n; j++) {
array[j][i] = n - f[j]; //top
array[i][j] = n - f[j]; //left
array[(2 * n - 1 - 1) - i][j] = n - f[i]; //bottem
array[j][(2 * n - 1 - 1) - i] = n - f[i]; //rigth
}
}
for(int i = 0; i < n - 1 + n; i++) {
for(int j = 0; j < n - 1 + n; j++) {
printf("%d ", array[i][j]);
}
printf("\n");
}
return 0;
}
my logic was to make all four borders correct in for loop which will end at center, but its not working, I want a new logic or to improve my logic, if you want to help me out then please give me the way to solve problem instead of giving me a direct code.
It is observable that the pattern consists of n stacked squares:
Square #0 is drawn with ns.
Square #1 is drawn with n-1s.
...
Square #n-1 is drawn with 1s.
Implementing the above:
void draw_pattern(const size_t n)
{
const size_t dim = 2*n-1;
int array[dim][dim];
for (size_t i = 0; i < n; ++i) { // Outer square #i
// Row #0 of the outer square #i
for (size_t j = i; j < dim-i; ++j)
array[i][j] = n-i;
// Row #n-1 of the outer square #i
for (size_t j = i; j < dim-i; ++j)
array[dim-i-1][j] = n-i;
// Col #0 of the outer square #i
for (size_t j = i; j < dim-i; ++j)
array[j][i] = n-i;
// Col #n-1 of the outer square #i
for (size_t j = i; j < dim-i; ++j)
array[j][dim-i-1] = n-i;
}
print_array(dim, array);
}
This is print_array():
void print_array(const size_t dim, int array[dim][dim])
{
for (size_t i = 0; i < dim; ++i) {
for(size_t j = 0; j < dim; ++j)
printf("%d ", array[i][j]);
printf("\n");
}
}
Output:
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4
The worst case time complexity is O(n2).
When you get a problem like this, try to dumb it down as much a possible. This square can be separated into 8 same, just rotated "slices" that look like:
4 | 4444 | 4444 | 4
43 | 333 | 333 | 34
432 | 22 | 22 | 234
4321 | 1 | 1 | 1234
... and the same for the bottom half, just flipped.
You can see this in the code bellow, to check what line is writing what part of the square, comment it and you will see what section shows zeroes.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d", &n);
int array[2 * n - 1][2 * n - 1];
for(int i = 0; i < 2 * n - 1; i++){
for(int j = 0; j < 2 * n - 1; j++){
array[i][j] = 0;
}
}
int f[n];
for(int i = 0; i < n; i++)
{
f[i] = i;
}
for(int i = 0; i < n; i++)
{
for(int j = i; j < n; j++)
{
array[i][j] = n - i;
array[j][i] = n - i;//top left
array[j][2*n - i - 2] = n - i;
array[i][2*n - j - 2] = n - i;//bottom left
array[2*n - j - 2][i] = n - i;
array[2*n - i - 2][j] = n - i;//top right
array[2*n - i - 2][2*n - j - 2] = n - i;
array[2*n - j - 2][2*n - i - 2] = n - i;//bottom right
}
}
for(int i = 0; i < n - 1 + n; i++)
{
for(int j = 0; j < n - 1 + n; j++)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
return 0;
}
#include<stdio.h>
int main()
{
int n ;
printf("Input the number of rows: ");
scanf("%d", &n);
for(int i = 1; i <= n ; i++)
{
for(int j = 1 ; j <= i ; j++)
{
if(j == i + 1)
{
break;
}
printf("%3d", j);
}
for(int j = i - 1 ; j > 0; j--)
{
if(j == 0)
{
break;
}
printf("%3d", j);
}
printf("\n");
}
}
Program session
Number of rows for this output n = 3
My output:
1
1 2 1
1 2 3 2 1
Preferred output:
1
1 2 1
1 2 3 2 1
This here is an exercise where I have to print a pyramid of numbers where the central number of the pyramid is the number of the row. I understand the logic but as you can see I have not been able to fulfill the task successfully. Any tips?
As pointed out by #WeatherWane, you need to add logic to add extra spaces. If you notice carefully, number of spaces on each line(excluding padding you add with %3d is equal to 3 * (n - i)). You can create a simple method like this to add spacing:
void addSpaces(int N, int currentIndex, int padding) {
for (int index = currentIndex; index < N; index++)
for (int spaces = 0; spaces < padding; spaces++)
printf(" ");
}
then you can call it from your first for loop like this:
for(int i = 1; i <= n ; i++)
{
addSpaces(n, i, 3);
for(int j = 1 ; j <= i ; j++)
{
// ...
I tested it and it seems to align it correctly:
c-posts : $ ./a.out
Input the number of rows: 5
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
Solved here is the code that worked for me
for (i = 0; i < nbLine; i++)
for (k = 0; k < zoom; k++)
{
for (j = 0; j < nbColumn; j++)
for (l = 0; l < zoom; l++)
{
printf("%d ", *(array+ (i*nbColumn) + j));
}
printf("\n");
}
I have a function that output a two dimensional array ([column][line]) and it need to be zoom, in fact it is like going from
1 2
3 4
to a
1 1 2 2
3 3 4 4 array when zoom is equal to 2
here is some code :
static void output(int* array, int nbColumn, int nbLine, int zoom)
{
int i, j, k, l;
for (i = 0; i < nbColumn; i++)
for (k = 0; k < zoom; k++)
{
for (j = 0; j < nbLine; j++)
for (l = 0; l < zoom; l++)
{
printf("%d ", *(array+ (i*nbColomn) + j));
}
printf("\n");
}
}
when I try this code on a squared array, it works fine, but when using a rectangular one, it fails. I have tried to debug it by replacing the printf("%d ", *(array + (i*nbColumn) + j)); by printf("%d ", (i*nbColumn) + j); and it gives me this result for a 8 colomns by 5 rows array :
0 1 2 3 4 5 6 7
5 6 7 8 9 10 11 12
10 11 12 13 14 15 16 17
15 16 17 18 19 20 21 22
20 21 22 23 24 25 26 27
Thanks for help
A working program source is:
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("in.in");
ofstream out("out.out");
int main(void){
int m[100][100],i,j,l,noLines,noColumns,zoom;
in>>noLines>>noColumns>>zoom;
for(i=0;i<noLines;i++){
for(j=0;j<noColumns;j++){
in>>m[i][j];
}
}
for (i = 0; i < noLines; i++){
for (j = 0; j < noColumns; j++){
for (l = 0; l < zoom; l++) {
out<<m[i][j]<<' ';
}
}
out<<'\n';
}
return 0;
}
On the input:
4 3 2
1 2 3 4
5 6 7 8
1 2 3 4
5 6 7 8
you get the output:
1 1 2 2 3 3
4 4 5 5 6 6
7 7 8 8 1 1
2 2 3 3 4 4
Changing the parameters on the first line of the input (i.e. the bidimensional array width, height and the zoom), changes the output.
From your example I can see that you are zooming only horizontally, since the elements are duplicated horizontally, but the number of rows is left intact. So you do not need vertical zoom and therefore your cycle with k is unuseful. This should work:
static void output(int* array, int nbColumn, int nbLine, int zoom)
{
int i, j, l;
for (i = 0; i < nbLine; i++)
{
for (j = 0; j < nbColumn; j++)
for (l = 0; l < zoom; l++)
{
printf("%d ", *(array+ (i*nbColomn) + j));
}
printf("\n");
}
}
Code
#include <stdio.h>
int main()
{
long long int n, m, p, i, j, total_cost, cost;
scanf("%lld %lld %lld", &n, &m, &p);
long long int ar[n][m];
for(i = 1; i <= n; ++i)
{
for(j = 1; j <= m; ++j)
{
ar[i][j] = j;
}
}
while(p--)
{
scanf("%lld %lld", &i, &j);
ar[i][j] += 1;
}
/*
printf("\n");
for(i = 1; i <= n; ++i)
{
for(j = 1; j <= m; ++j)
{
printf("%d ", ar[i][j]);
}
printf("\n");
}
printf("\n");
*/
if(n == 1 && m == 1)
{
printf("0\n");
return 0;
}
if(m == 1)
{
for(i = 1; i <= n; ++i)
{
printf("0\n");
}
return 0;
}
for(i = 1; i <= n; ++i)
{
total_cost = 0, cost = 0;
for(j = m; j >= 2; --j)
{
cost = ar[i][j] - ar[i][j - 1];
if( cost < 0 )
{
printf("-1\n");
break;
}
total_cost += cost;
}
if(cost >= 0)
{
printf("%lld\n", total_cost);
}
}
return 0;
}
This is giving me WA. Here is the problem description CHEFBM.
Test cases I have checked:
1. 1 5 3
1 2
1 4
1 5
Output
5
2. 4 4 6
2 2
3 2
3 2
4 3
4 4
4 3
Output
3
3
-1
4
3. 1 4 5
1 3
1 2
1 2
1 1
1 1
Output
1
4. 4 4 6
3 2
2 3
3 2
2 4
1 2
3 2
Output
3
4
3
3
5. 4 1 1
1 1
Output
0
0
0
0
6. 1 4 3
1 1
1 1
1 4
Output
-1
7. 1 1 1
1 1
Output
0
8. 1 1 2
1 1
1 1
Output
0
9. 2 2 3
1 1
1 1
1 1
Output
-1
1
And many more. For which test case is this giving wrong answer?
As given in the constraints, n and m both can be 10^5. Allocating a 2-d array a[10^5][10^5], is not feasible here. You are running out of memory and hence you might be getting Run time error or Wrong answer. Your algorithm is O(n^2) so it will not pass in the given time limit. Try to optimize your code.
For further assistance, you can check my code.