Question:
Given a matrix A, compute the sum of maximal elements row-wise (find a maximum element of each row and take their sum) and column-wise independently and return those values.
In which input is going to be :
First-line contains n, m number of rows and columns of input matrix.
Next n lines contains m integers.
Some examples:
Input1:
3 3
4 3 2
3 7 7
2 6 0
Output1:
17 18
Input2:
3 4
1 2 3 4
5 6 7 8
9 10 11 12
Output2:
24 42
I can't tell how I am supposed to take the matrix input using scanf. Please help.
just use scanf("%d", &variable) no matter if delimiter is a new line or a space:
#include <stdio.h>
int main()
{
int lines, cols;
scanf("%d", &lines);
scanf("%d", &cols);
for(int y = 0; y < lines; y++){
for(int x = 0; x < lines; x++){
int variable;
scanf("%d", &variable);
// your code
}
}
// your code
return 0;
}
Related
I have this assignment to get and transpose a matrix using dynamic memory allocation in C
I did it by converting the linear position to (i,j) and swapping i,j
old and new element positions are perfect,
somehow the swap is not working as i intended,
might seem like i'm making others problem solve for me, but i'm blank at this point so help will be really appreciated
Here's the code:
#include <stdio.h>
#include <stdlib.h>
int main(){
int m,n;
printf("Enter the order of matrix, m*n:\n");
scanf("%d %d",&m,&n);
int *matrix_ptr;
matrix_ptr = (int *) malloc(m*n*sizeof(int));
printf("Enter the elements of %d*%d matrix\n",m,n);
for(int i=0; i<m*n; i++){
scanf("%d", matrix_ptr+i);
}
// Transposing the matrix
for(int i=0; i<m*n; i++){
int i_index = i / n;
int j_index = i % n;
// (i_index)*n + j_index gives the linear position
int new_linear_pos = (j_index)*n + i_index;
int temp = *(matrix_ptr + new_linear_pos);
*(matrix_ptr + new_linear_pos) = *(matrix_ptr + i);
*(matrix_ptr + i) = temp;
if(i==0){
printf("\nThe transpose is:\n");
}
printf("%d ", *(matrix_ptr+i));
if((i+1)%n == 0){
printf("\n");
}
}
}
The output:
You are swapping all values twice and you are printing the ones at the beginning of the line after the second swap. The first swap happened with i equal 1 and 2
Let's say you have this matrix at the begin:
1 2 3
4 5 6
7 8 9
swap index 0 with 0 stays the same thing. Prints 1
swap index 1 with 3: prints 4
1 4 3
2 5 6
7 8 9
swap index 2 with 6: prints 7\n
1 4 7
2 5 6
3 8 9
swap index 3 with 1: prints 4
1 2 7
4 5 6
3 8 9
etc...
The solution would be to swap elements only once.
The easiest fix would be a if (i > new_linear_pos) continue; //already swapped
I don't understand how this code displays a triangle. My main problem is understanding the working of integers j and k. How do they affect the triangle's position?
#include <stdio.h>
int main(){
int j,k,Rows;
printf("Enter The Number of Rows : ");
scanf("%d", &Rows);
for(j=2;j<=l;j++){
for(k=1;k<=j;k++){
printf(" %d ", j);
}
printf("\n");
}
return 0;
}
I guess the program you want is
#include <stdio.h>
int main(){
int j,k,Rows;
printf("Enter The Number of Rows : ");
scanf("%d", &Rows);
for(j=1;j<=Rows;j++){
for(k=1;k<=j;k++){
printf(" %d ", j);
}
printf("\n");
}
return 0;
}
Changes are:
Change l to Rows
Change j=2 to j=1
And here is an example result
Enter The Number of Rows : 6
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
My main problem is understanding the working of integers j and k. How do they affect the triangle's position?
j here can represent the row index. k can represent the column index. In another word, k represents how many items in current row. for(k=1;k<=j;k++) means the max value of k is j. Since j is increased by 1, so the max value of k is also increased by 1:
口 # j = 1, the max value of k is 1, so this row has 1 item.
口 口 # j = 2, the max value of k is 2, so this row has 2 items.
口 口 口 # j = 3, the max value of k is 3, so this row has 3 items.
口 口 口 口 # j = 4, the max value of k is 4, so this row has 4 items.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have an array with randomly generated numbers in it, and I need to find and store the numbers that are present at least x times. (I can easily find x, that is not important. It will be a set amount). The array will have around 100-200 elements.
I know I will have to use a loop, but I'm not sure how to structure the body of it. I'm relatively new to C programming, so this may be fairly trivial. Any help is appreciated.
I implemented two solutions, one in C and one in C++.
First solution in C:
It is simple:
Input array of numbers. Either it is inputed from console (when input = 1) or generated automatically by random generator (set constant input = 0 in first lines of main()). Input/generation is done by first loop. If generated automatically/randomly then set constants n (number of elements) and x (minimal allowed count) at first lines of main() function.
Sort them in ascending order by built-in function qsort. I provided compare() function for qsort to compare in ascending order.
Iterate through sorted array. On the way compute number c of consecutive equal numbers. If c >= x then it means we have at least x of consecutive equal numbers, it means this number occured at least x times, hence we output such number as matching our criteria.
Try it online!
#include <stdlib.h>
#include <stdio.h>
static int compare(void const * sa, void const * sb) {
int a = *(int*)sa, b = *(int*)sb;
return a < b ? -1 : a == b ? 0 : 1;
}
int main() {
// "n" - total number of integers, "x" - minimum count to find
// "input" - if 1 then read from console input array, else if 0 generate random
int const n = 50, x = 3, input = 1;
// Example of input: n = 50, x = 3, a = 3 6 17 15 13 15 6 12 9 1 2 7 10 19 3 6 0 6 12 16 11 8 7 9 2 10 2 3 7 15 9 2 2 18 9 7 13 16 11 2 9 13 1 19 4 17 18 4 15 10
// and output for this example: 2: 6 times, 3: 3 times, 6: 4 times, 7: 4 times, 9: 5 times, 10: 3 times, 13: 3 times, 15: 4 times,
if (input) {
printf("Input n and x: ");
scanf("%d %d", &n, &x);
printf("Input array: ");
} else
srand(0); // You may init this with some randomness
int * a = (int*)malloc(sizeof(int) * n); // Allocate memory for empty array
for (int i = 0; i < n; ++i) {
if (input)
scanf("%d", &a[i]);
else {
a[i] = rand() % 20; // Init numbers with random, you may input your numbers
printf("%d ", a[i]); // Print generated numbers
}
}
if (!input)
printf("\n");
qsort(a, n, sizeof(int), compare);
int c = 1;
for (int i = 1; i <= n; ++i) {
if (i == n || a[i] != a[i - 1]) {
if (c >= x)
printf("%d: %d times, ", a[i - 1], c);
c = 1;
} else
++c;
}
free(a);
return 0;
}
Input:
50 3
3 6 17 15 13 15 6 12 9 1 2 7 10 19 3 6 0 6 12 16 11 8 7 9 2 10 2 3 7 15 9 2 2 18 9 7 13 16 11 2 9 13 1 19 4 17 18 4 15 10
Second solution in C++:
Use std::map to count number of occurances of each number, by iterating (in a loop) over input array and incrementing by 1 count of current nummber inside map, then iterate (in a second loop) through this map and output those numbers that occur at least x times.
Also I used std::map specifically to have nice sorted output of numbers, if you don't need sorted order and need more speed then use std::unordered_map instead of std::map exactly same way (also do #include <unordered_map> instead of <map>).
Full code is below:
Try it online!
#include <iostream>
#include <cstdlib>
#include <vector>
#include <map>
using namespace std;
int main() {
size_t const n = 50; // How many numbers
size_t const x = 3; // At least this many times
srand(0); // Use this if only deterministic randoms are needed
vector<int> a(n); // Numbers
for (size_t i = 0; i < a.size(); ++i) {
a[i] = rand() % 20; // Some random numbers
cout << a[i] << " "; // Output our input numbers
}
cout << endl;
map<int, size_t> cnt;
// Count number of occurances
for (auto x: a)
++cnt[x];
// Output numbers that occur at least "x" times
for (auto const & p: cnt)
if (p.second >= x)
cout << p.first << ": " << p.second << " times, ";
return 0;
}
Output:
3 6 17 15 13 15 6 12 9 1 2 7 10 19 3 6 0 6 12 16 11 8 7 9 2 10 2 3 7 15 9 2 2 18 9 7 13 16 11 2 9 13 1 19 4 17 18 4 15 10
2: 6 times, 3: 3 times, 6: 4 times, 7: 4 times, 9: 5 times, 10: 3 times, 13: 3 times, 15: 4 times,
I am working on an exercise "k largest elements" from geeksforgeeks.org
https://practice.geeksforgeeks.org/problems/k-largest-elements/0
The task:
Given an array of N positive integers, print k largest elements from the array. The output elements should be printed in decreasing order.
Input:
The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N and k, N is the size of array and K is the largest elements to be returned. The second line of each test case contains N input C[i].
Output:
Print the k largest element in descending order.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 100 (actually 1000 as someone identified)
K ≤ N
1 ≤ C[i] ≤ 1000*
Example:
Input:
2
5 2
12 5 787 1 23
7 3
1 23 12 9 30 2 50
Output:
787 23
50 30 23
Explanation:
Testcase 1: 1st largest element in the array is 787 and second largest is 23.
Testcase 2: 3 Largest element in the array are 50, 30 and 23.
I have constructed a solution, which works with the initial test case, but later throws a segmentation fault. I cannot understand where could I get the segmentation fault:
Runtime Error:
Segmentation Fault (SIGSEGV)
Runtime Error
The problem with these exercises is that I do not know the input data to the test case, which generates the error. Bellow you will find my code, which is rather simple. Perhaps you can help me to identify what could cause the segmentation fault.
#include <stdio.h>
static int Arr[1024], Res[1024];
int main()
{
int t=0;
scanf("%d", &t);
while(t--)
{
int n=0,k=0, i=0,j=0,z=0;
scanf("%d %d", &n, &k);
// if(n>1000)
// printf("Gotya");
for(i=0; i<n; i++)
{
scanf("%d", &Arr[i]);
}
i=0;
int max_l = 0, max_h = 1000, j_max = 0;
for(i=0; i<k; i++)
{
for(j=0; j<n; j++)
{
if(Arr[j] >= max_l)
{
max_l = Arr[j];
j_max = j;
}
}
Res[i]= max_l;
max_l = 0;
Arr[j_max] = 0;
j_max = 0;
}
for(z=0; z<k; z++)
{
printf("%d ", Res[z]);
}
printf("\n");
}
return 0;
}
This site does not seem to be abiding by their constraints.
I made a login and modified the code to print if N>1000. The value of N is 20567
Wrong Answer. !!!Wrong Answer
Possibly your code doesn't work correctly for multiple test-cases (TCs).
The first test case where your code failed:
Input:
20567 18428
9737 16220 4527 21952 22174 12861 29801 8125 13670 9713 5742 14988 31137 21891 25646 18474 18286 30312 6105 19031 18587 15877 14546 29756 18364 24690 12129 16209 378 27774 16552 18302 8238 10483 1752 12929 5551 22299 14756 9871 18279 32386 23286 5182 16557 9726 7137 22434 24399 7661 3544 29878 11885 16318 29725 29438 25486 18099 18811 7275 12781 9700 20024 9087 26644 32648 12870 29873 2988 8560 12217 14099 26516 10964 10622 16434 16432 16210 8777 32574 8708 27444 8143 9067 32385 7410 20022 406 2846 22519 30665 32044 5803 1192 9457 30792 18658 419 30816 3867 64 23108 7056 8849 16915 18030 20332 30257 28883 22408 30029 25926 15541 30405 9255 29500 638 6313 7685 15180 3221 18889 14770 860 27229 30930 5305 30240 20929 8652 8592 18566 16145 16032 26129 20049 18188 8408 32297 16899 20286 6311 14036 17409 8332 3369 21833 21746 2061 27115 24177 20328 31259 1454 29342 9410 27562 26216 4474 24913 21977 1676 23102 15289 6087 32651 7204 13210 10920 15771 19230 880 23576 8375 11464 2706.................
Its Correct output is:
32768 32768 32766 32765 32761 32759 32757 32756 32756 32755 32754 32752 32747 32747 32747 32746 32743 32742 32741 32740 32739 32738 32738 32735 32735 32735 32733 32733 32732 32731 32730 32728 32728 32726 32726 32726 32725 32724 32719 32718 32718 32716 32714 32714 32708 32706 32706 32698 32694 32694 32691 32690 32690 32689 32688 32687 32684 32682 32676 32675 32672 32670 32663 32660 32658 32657 32657 32656 32651 32648 32646 32645 32644 32643 32638 32637 32637 32637 32637 32635 32632 32630 32630 32625 32625 32625 32623 32622 32622 32620 32619 32618 32616 32614 32614 32613 32613 32612 32608 32606 32606 32605 32598 32596 32593 32591 32589 32588 32586 32586 32586 32582 32582 32581 32580 32580 32579 32578 32574 32573 32571 32571 32567 32567 32566 32564 32564 32563 32562 32560 32560 32559 32559 32558 32555 32554 32552 32551 32551 32551 32550 32549 32549 32547 32547 32546 32544 32544 32543 32543 32540 32539 32538 32538 32537 32535 32533 32533 32533 32532 32531 32528 32527 32527 32526 32525 3252.................
And Your Code's output is:
Gotya
However, with the correct length of the array Arr and Res, you get an error of
Expected Time Limit < 1.996sec
Hint : Please optimize your code and submit again.
I leave this up to you.
Hint - Use qsort for a better sorting performance.
A set of points over a straight line is defined as correlative to some K if the absolute difference between any two points is a multiple of K. Given N (2 <= N <= 100000) points and some integer K (1 <= K <= 1000). Your task is to find the largest set which is correlative to K. You can assume that only one largest set exists. N and K will be in the first line of the input. N lines will follow, each one with a single integer, representing the location of one of the points. Print the size of the largest set of points which is correlative to K, in the first line of the input. Remaining lines will contain the points of the set, one per line, in increasing order.
Case 1:
For the input provided as follows:
5 2
1
2
3
4
5
Output of the program will be:
3
1
3
5
Case 2:
For the input provided as follows:
6 4
10
15
12
16
20
32
Output of the program will be:
4
12
16
20
32
#include <bits/stdc++.h>
using namespace std;
// function to find remainder set
int findSet(int arr[], int n, int k, int m) {
vector remainder_set[k];
// calculate remainder set array
// and push element as per their remainder
for (int i = 0; i < n; i++) {
int rem = arr[i] % k;
remainder_set[rem].push_back(arr[i]);
}
// check whether sizeof any remainder set
// is equal or greater than m
for (int i = 0; i < k; i++) {
if (remainder_set[i].size() >= m) {
cout <<m<< "\n";
for (int j = 0; j < m; j++){
cout << remainder_set[i][j] << "\n";
}
return 1;
}
}
return 0;
}
// driver program
int main() {
int n,k;
cin>>n>>k;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
int z;
int m = sizeof(arr)/sizeof(int);
for(int i=m;i>0;i--)
{
z=findSet(arr, n, k, i);
if(z==1)
break;
}
}
This program prompts the user a number and then outputs a table of even squares ranging from 2 to the number.
#include <stdio.h>
int main(void)
{
int i, n;
puts(This program prints a table of even squares.);
printf("Enter range of the squares square: ");
scanf("%d", &n);
for (i = 2; i * i <= n; i += 2)
printf("%d\n", i * i);
return 0;
}
for example:
Enter range of the squares: 123
2 4
4 16
6 36
8 64
10 100
The problem is did not print 121 (which is 11 * 11). I am new to C and not really good in using loops. Please help!
for (i = 2; i * i <= n; i += 2)
You're starting at 2 and incrementing by 2. i will never be 11.