output is printing only few elements of matrix that to in a wrong order
i think something is wrong with the printing loop but unable to figure it out
#include <stdio.h>
int main()
{
int x=0, y=0;
int a[x][y];
printf("enter the number of rows in first matrix:\n");
scanf("%d", &x);
printf("enter the number of columns in first matrix:\n");
scanf("%d", &y);
printf("enter elements of first matrix\n");
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
printf("enter element %d %d\n", i, j);
scanf("%d", &a[i][j]);
}
}
for (int p = 0; p < x; p++)
{
for (int q = 0; q < y; q++)
{
printf("%d\t", a[p][q]);
}
printf("\n");
}
return 0;
}
You're declaring the matrix before receiving the size of the matrix (you're variables x and y). In your code, the matrix is declared as a[0][0].
Solution:
#include <stdio.h>
int main() {
int x, y;
printf("enter the number of rows in first matrix:\n");
scanf("%d", &x);
printf("enter the number of columns in first matrix:\n");
scanf("%d", &y);
int a[x][y];
printf("enter elements of first matrix\n");
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
printf("enter element %d %d\n", i, j);
scanf("%d", &a[i][j]);
}
}
for (int p = 0; p < x; p++) {
for (int q = 0; q < y; q++) {
printf("%d\t", a[p][q]);
}
printf("\n");
}
return 0;
}
You're initializing your 2d array with 0 rows and 0 columns.
You need to move int a[x][y] down under scanf("%d", &y);
#include <stdio.h>
int main() {
int x, y;
printf("enter the number of rows in first matrix:\n");
scanf("%d", &x);
printf("enter the number of columns in first matrix:\n");
scanf("%d", &y);
int a[x][y];
printf("enter elements of first matrix\n");
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
printf("enter element %d %d\n", i, j);
scanf("%d", &a[i][j]);
}
}
for (int p = 0; p < x; p++) {
for (int q = 0; q < y; q++) {
printf("%d\t", a[p][q]);
}
printf("\n");
}
return 0;
}
Related
#include <stdio.h>
#include <stdlib.h>
int main()
{
int N, K, i;
printf("Enter size of array: ");
scanf("%d", &N);
printf("Please enter value of K: ");
scanf("%d", &K);
int arr[N];
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < N; i++)
{
int temp = arr[i];
arr[i + K] = arr[i];
arr[i] = temp;
}
for (i = 0; i < N; i++)
{
printf("%d", arr[i]);
}
return 0;
}
I know the current code is totally wrong It was just another test.
Basically what I need to do is something like this:
the array before: arr[N]={1,2,3,4,5,6,7,8,9,10}
the array after if K is 2: arr[N]={10,9,1,2,3,4,5,6,7,8}
Like #whozcraig pointed out for in-place rotation of array members.
Define a function to reverse(in-place) array members in a range :
static inline void
arr_reverse (const int start, const int end, int arr[]) {
for (int ai = start, zi = end; ai < zi; ++ai, --zi) {
int tmp = arr[ai];
arr[ai] = arr[zi];
arr[zi] = tmp;
}
}
Then you call it like :
K %= N;
if (K != 0) {
arr_reverse (0, N-1, arr);
arr_reverse (0, K-1, arr);
arr_reverse (K, N-1, arr);
}
I write something like this but it creates new array instead of modifying current one but it works.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int N, K, i;
printf("Enter size of array: ");
scanf("%d", &N);
printf("Please enter value of K: ");
scanf("%d", &K);
int arr[N], newArr[N];
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < K; i++)
{
newArr[i] = arr[N-1-i];
}
for (int x = 0; i < N; i++)
{
newArr[i] = arr[x];
x++;
}
for (i = 0; i < N; i++)
{
printf("%d ", newArr[i]);
}
return 0;
}
Thank you guys, for all the comments and answers. I've tried them and they worked.
I have found a way to do it as well. It's bit different. I did it with a function which moves all the elements with 1 position and then repeat the func as much as needed (K times).
#Cheatah helped me come up with it. I will post it in case somebody likes this solution in the future.
Here it is:
#include <stdio.h>
#include <stdlib.h>
int moveOnePos(int num, int array[num])
{
int temp = array[num - 1];
for (int b = num - 1; b > 0; b--)
{
array[b] = array[b - 1];
}
array[0] = temp;
return 0;
}
int main()
{
int N, K, i;
printf("Please enter size of array: ");
scanf("%d", &N);
printf("Please enter K: ");
scanf("%d", &K);
int arr[N];
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < K; i++)
{
moveOnePos(N, arr);
}
for (i = 0; i < N; i++)
{
printf("%d", arr[i]);
}
return 0;
}
This program gives me zero primes in the array after running it. This is a program that takes 2d array [n x m] then calculate how many prime numbers are there in the 2d array.
int isprime(int n)
{
int k;
if (n <= 1)
return 0;
for (k = 2; k <= (n / 2); k++){
if (n % k == 0)
return 0;
}
return 1;
}
}
int primecount(int x, int y, int a[x][y]){
int r, c, count = 0;
for(r = 0; r < x; r++) {
for(c = 0; c < y; c++) {
if(isprime(a[r][c]))
{
count++;
}
}
}
return count;
}
int main()
{
int n, m, i, j, z;
printf("Enter the Number of Rows: ");
scanf("%d", &n);
printf("\n");
printf("Enter the Number of Columns: ");
scanf("%d", &m);
printf("\n");
int a[n][m];
printf("Enter the elements of the array: \n");
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
scanf("%d", &a[i][j]);
}
z = primecount(n, m, a[n][m]);
printf("\n");
printf("The Number of Prime Numbers in the array is: %d", z);
printf("\n");
return 0;
}
For starters instead of this call
z = primecount(n, m, a[n][m]);
you need to write
z = primecount(n, m, a);
In this call of the function isprime as in the call shown above
if(isprime(r, c, a[r][c]))
the expression a[r][c] is a scalar object of the type int. However the function isprime expects a two-dimensional array instead of a scalar object of the type int.
int isprime(int p, int q, int a[p][q])
^^^^^^^^^^^
Just declare the function like
int isprime( int x );
and correspondingly change its definition.
The function will be called like
if( isprime( a[r][c] ) )
Pay attention to that the logic of the function isprime is incorrect. It returns logical true for values equal to 1 and 0 though such values are not prime numbers.
Also you need to deal with an array with elements of the type unsigned int. Otherwise the user can enter negative values.
Here is your updated program.
#include <stdio.h>
int isprime(int n)
{
int k;
if (n <= 1)
return 0;
for (k = 2; k <= (n / 2); k++)
{
if (n % k == 0)
return 0;
}
return 1;
}
int primecount(int x, int y, int a[x][y]) //function to count prime numbers
{
int r, c, count = 0;
for(r = 0; r < x; r++)
{
for(c = 0; c < y; c++)
{
if(isprime(a[r][c]))
{
count++;
}
}
}
return count;
}
int main()
{
int n, m, i, j, z;
printf("Enter the Number of Rows: ");
scanf("%d", &n);
printf("\n");
printf("Enter the Number of Columns: ");
scanf("%d", &m);
printf("\n");
int a[n][m];
printf("Enter the elements of the array: \n");
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
scanf("%d", &a[i][j]);
}
z = primecount(n, m, a);
printf("\n");
printf("The Number of Prime Numbers in the array is: %d", z);
printf("\n");
return 0;
}
Its output might look like
Enter the Number of Rows: 2
Enter the Number of Columns: 2
Enter the elements of the array: 1 2 3 4
The Number of Prime Numbers in the array is: 2
Hello I'm trying to write a program about subsets and my problem is that whenever I try to run the program, it's automatically saying that "Y is a not subset of X" even though it is a subset. Can someone help me with this? This is my code:
#include<stdio.h>
int main()
{
int i, j, size1, size2, flag;
printf("S U B S E T S\n\n");
printf("Enter number of digits for Array X: ");
scanf("%d", &size1);
int array1[size1];
printf("Enter number of digits for Array Y: ");
scanf("%d", &size2);
int array2[size2];
printf("\n");
printf("Enter %d digits for Array X: ", size1);
for(i = 0; i < size1; i++)
{
scanf("%d", &array1[i]);
}
printf("\n");
printf("Enter %d digits for Array Y: ", size2);
for(i = 0; i < size2; i++)
{
scanf("%d", &array2[i]);
}
printf("\n\n");
for(i = 0; i < size2; i++)
for(j = 0; j < size1; j++)
if(array2[i] == array1[j])
flag++;
if(flag == size1)
{
printf("Array X is a subset of Array Y");
}
else
{
printf("Array X is not a subset of Array Y");
}
return 0;
}
It is working with very minor changes. Just loop from 0 and use < instead of <=.
#include<stdio.h>
int isSubset(int arr1[], int arr2[], int m, int n) {
int i = 0;
int j = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (arr2[i] == arr1[j])
break;
}
if (j == m)
return 0;
}
return 1;
}
int main() {
int arr1[20];
int arr2[20];
int m, n;
printf("How many elements of X you want to store?\n");
scanf("%d", &m);
printf("How many elements of Y you want to store?\n");
scanf("%d", &n);
for (int i = 0; i < m; i++) {
printf("\nEnter %d X values to be stored:\n", m);
scanf("%d", &arr1[i]);
}
for (int j = 0; j < n; j++) {
printf("\nEnter %d Y values to be stored:\n", n);
scanf("%d", &arr2[j]);
}
m = sizeof(arr1) / sizeof(arr1[0]);
n = sizeof(arr2) / sizeof(arr2[0]);
if (isSubset(arr1, arr2, m, n))
printf("Y is a subset of X \n");
else
printf("Y is not a subset of X\n");
getchar();
return 0;
}
I have to find out if there is any pair of i,j such that array[i]^2 + array[j]^2 == x^2
.
If there are such pairs, I need to print all such (i,j). Otherwise, print “There are no such pairs”.
#include <stdio.h>
int main(){
int size=10, i, x,j;
int Array[size];
printf("What is the value of x:");
scanf("%d",&x);
for(i=0;i<size;i++){
printf("Enter array value :");
scanf("%d",&Array[i]);
}
for(i=0;i<size;){
for(j=i+1;j<size;j++)
if((Array[i]*Array[i])+(Array[j]*Array[j])==x*x) //how do I complete this for loop?
}
return 0;
}
Yo're almost there, why weren't you incrementing the value of i? Keep a counter to count the matched pairs, then print those or if nothing is found print whatever you want.
#include <stdio.h>
int main() {
int size = 10, i, x, j;
int Array[size];
printf("What is the value of x:");
scanf("%d", &x);
for (i = 0; i < size; i++) {
printf("Enter array value :");
scanf("%d", &Array[i]);
}
int counter = 0;
for (i = 0; i < size; i++) {
for (j = i + 1; j < size; j++)
if ((Array[i] * Array[i]) + (Array[j] * Array[j]) == x * x) {
printf("%d %d\n", Array[i], Array[j]);
counter++;
}
}
if (!counter) {
printf("There are no such pairs\n");
}
return 0;
}
So i've been trying to do a code that adds the integers of a row in an array (this integers can be negative for example)
So you will have something like this:
input:
3 2 (Number of rows and columns)
12 7
-3 6
-2 -5
output:
19
3
-7
What I've done is this:
#include <stdio.h>
int main ()
{
static int array[10][10];
int i, j, m, n, sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("%d\n",sum);
sum = 0;
}
}
I can't find a way to "add" negative numbers, what should I change/add to this code? Thanks again :)
#include <stdio.h>
int main (){
int array[10][10];
int i, j, m, n, sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i){
for (j = 0; j < n; ++j){
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i){
for (j = 0; j < n; ++j){
sum = sum + array[i][j] ;
}
printf("%d\n",sum);
sum = 0;
}
return 0;
}