I'm trying to multiply a number that i choose to a multidimensional array with values that i have chose, but my code doesn't do that.
Here it is the multiplication function:
int product_array(){
i = 0;
j = 0;
int number;
int product[i][j];
printf("Insert the number\n");
scanf("%d", &number);
while (i < rows) {
printf("\n");
j = 0;
while (j < columns) {
product[i][j] = number + product[0][0] * m_array[i][j];
j++;
}
i++;
}
i = 0;
j = 0;
printf("Here it is the product\n");
while (i < rows) {
printf("\n");
j = 0;
while (j < columns) {
printf("\t%d ", product[i][j]);
j++;
}
i++;
printf("\n");
}
return product[i][j];
}
Legend: m_array means multidimensional array
Example of input ad output
Input
m_array values = 2 2 2 2
Number = 2
Output
4 4 4 4
To be clearer this is the piece of code with which the user can insert the values into the m_array:
do {
printf("How many rows will be in the array?\n");
scanf("%d", &righe);
} while (rows > Row || rows < 1);
do {
printf("How many columns will be in the array?\n");
scanf("%d", &columns);
} while (columns > Column || columns < 1);
printf("Data input\n");
while (i < rows) {
j=0;
while (j < columns) {
printf("Insert the element in the %d column and %d row\n", i, j);
scanf("%d", &m_array[i][j]);
j++;
}
i++;
}
Ok, so I figured out what the problem was (pretty simple), here is the code with the solution:
int product_array(){
i = 0;
j = 0;
int number;
printf("Insert the number\n");
scanf("%d", &number);
while (i < rows) {
printf("\n");
j = 0;
while (j < columns) {
m_array[i][j] = number * m_array[i][j];
j++;
}
i++;
}
i = 0;
j = 0;
printf("Here it is the product\n");
while (i < rows) {
printf("\n");
j = 0;
while (j < columns) {
printf("\t%d ", m_array[i][j]);
j++;
}
i++;
printf("\n");
}
return m_array[i][j];
}
Related
How can I display the input user of this code. I am using bubble sort but I cannot display the input.
Here's my code:
#include <stdio.h>
int main(void) {
int array[100];
int count;
for(int i = 0; i < 100; i++) {
printf("Enter number %d: ", i + 1);
scanf("%d", &array[i]);
if(array[i] == 0) break;
}
int size = sizeof(array) / sizeof(array[0]);
for(int i = 0; i < size - 1; ++i) {
int swapped = 0;
for(int ctr = 0; ctr < size - i - 1; ++ctr) {
if(array[ctr] > array[ctr + 1]) {
int temp = array[ctr];
array[ctr] = array[ctr + 1];
array[ctr + 1] = temp;
swapped = 1;
}
}
if(swapped == 0) {
break;
}
}
printf("[");
for(int i = 0; i < size; i++) {
if(i == size - 1) {
printf("%d", array[i]);
}
else {
printf("%d, ", array[i]);
}
}
printf("]");
return 0;
}
Here's the output:
Enter·number·1:·1
Enter·number·2:·2
Enter·number·3:·3
Enter·number·4:·4
Enter·number·5:·5
Enter·number·6:·0
[1,·2,·3,·4,·5]
I cannot display the 1 2 3 4 5.... How can I display?
The calculation of the size doesn't work like you did it. sizeof(array) will give you the total amount of of bytes of the array (of all 100 spaces). So your size will allway be 100.
Instead of calculating the size it's easyer to count the inputs like this:
int size=0;
for(int i = 0; i < 100; i++) {
printf("Enter number %d: ", i + 1);
scanf("%d", &array[i]);
if(array[i] == 0) break;
size++;
}
The user can enter i values in the array due to the break statement within the for loop
for(int i = 0; i < 100; i++) {
printf("Enter number %d: ", i + 1);
scanf("%d", &array[i]);
if(array[i] == 0) break;
}
So you need to keep this value and use it the following for loops because it can differ from the value of the expression sizeof(array) / sizeof(array[0])
hi this is my code can you help me out not printing output properly how to print output given in description , i am not getting way to sort the given output, output should be sorted from given average.
Implement fragments using array of pointers.
Rows are static and columns are dynamic. Fixed no.of rows and columns will vary for each row.
Example:
Read no.of rows from user and allocate the memory statically for rows.
Read no.of columns for each row and allocate the memory dynamically.
Let us Assume, Row = 3.
Row[0] = 4 columns, Row[1] = 3 columns and Row[2] = 5 columns.
While allocating the memory for columns you have allocate for no.of columns + 1 dynamically.
After that read the values from user and calculate the average for each row separately and store that average in that extra memory block which you added while allocating the memory.
Then sort the array based on the average.
Print the output on the screen.
Example is given below.
ENTER THE NUMBER OF ARRAY YOU WANT TO GIVE AS INPUT: 3
ENTER NO. OF COLUMNS IN ROW[0]: 3
ENTER NO. OF COLUMNS IN ROW[1]: 3
ENTER NO. OF COLUMNS IN ROW[2]: 3
ENTER 3 VALUES OF ROW[0]: 2 3 5
ENTER 3 VALUES OF ROW[1]: 2 4 7
ENTER 3 VALUES OF ROW[2]: 2 1 0
BEFORE SORTING OUTPUT IS:
2 3 5 3.33333
2 4 7 4.33333
2 1 0 1
AFTER SORTING OUTPUT IS:
2 1 0 1
2 3 5 3.33333
2 4 7 4.33333
My code so far:
#include <stdio.h>
#include <stdlib.h>
int fragments(int rows, int *array)
{
int i, j;
int pos[rows];
float *arr[rows], avg, sum;
char *temp[rows];
for (i = 0; i < rows; i++)
{
arr[i] = malloc(sizeof(float*) * array[i]+1);
pos[i] = i;
printf("Enter %d values for row[%d]: ", array[i], i);
for (j = 0; j< array[i]; j++)
{
scanf(" %f", &arr[i][j]);
}
}
printf("Before Sorting output is:\n");
for (i = 0; i < rows; i++)
{
sum = 0;
for (j = 0; j < array[i]; j++)
{
printf("%f ", arr[i][j]);
sum = sum + arr[i][j];
}
printf("%f\n", arr[i][j] = (sum / j));
}
for (i = 0; i < rows - 1; i++)
{
for (j = 0; j < rows - i - 1; j++)
{
if (arr[j][array[j]] > arr[j+1][array[j+1]])
{
temp[j][j] = arr[j][array[j]];
arr[j][array[j]] = arr[j+1][array[j+1]];
arr[j+1][array[j+1]] = temp[j][j];
}
}
}
for (i = 0; i < rows; i++)
{
for (j = 0; j < array[i] + 1; j++)
{
printf("%f ", arr[i][array[j]]);
}
printf("\n");
}
}
int main()
{
int rows, i, j;
printf("Enter no of rows: ");
scanf("%d", &rows);
int array[rows];
int pos[rows];
for (i = 0; i < rows; i++)
{
printf("Enter no of columns in row[%d]:", i);
scanf("%d", &array[i] );
}
fragments(rows, array); //`enter code here`
}
There are multiple problems in your code:
The allocation size is incorrect: arr[i] = malloc(sizeof(float*) * array[i]+1); should read:
arr[i] = malloc(sizeof(float) * (array[i] + 1));
the swapping code should swap the array pointers and the array sizes. The current code does not make sense. Use this instead:
float *temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
int temp1 = array[j];
array[j] = array[j+1];
array[j+1] = temp1;
the final print loop is incorrect: instead of printf("%f ", arr[i][array[j]]); you should write:
printf("%f ", arr[i][j]);
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
int fragments(int rows, int *array) {
float *arr[rows];
for (int i = 0; i < rows; i++) {
arr[i] = malloc(sizeof(float) * (array[i] + 1));
printf("Enter %d values for row[%d]: ", array[i], i);
for (int j = 0; j < array[i]; j++) {
if (scanf("%f", &arr[i][j]) != 1)
return;
}
}
printf("Before Sorting output is:\n");
for (int i = 0; i < rows; i++) {
float sum = 0;
for (j = 0; j < array[i]; j++) {
printf("%f ", arr[i][j]);
sum += arr[i][j];
}
printf("%f\n", arr[i][j] = sum / j);
}
for (int i = 0; i < rows - 1; i++) {
for (int j = 0; j < rows - i - 1; j++) {
if (arr[j][array[j]] > arr[j+1][array[j+1]]) {
float *temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
int temp1 = array[j];
array[j] = array[j+1];
array[j+1] = temp1;
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < array[i] + 1; j++) {
printf("%f ", arr[i][j]);
}
printf("\n");
}
for (int i = 0; i < rows; i++) {
free(arr[i]);
}
}
int main() {
int rows;
printf("Enter no of rows: ");
if (scanf("%d", &rows) != 1)
return 1;
int array[rows];
for (int i = 0; i < rows; i++) {
printf("Enter no of columns in row[%d]:", i);
if (scanf("%d", &array[i]) != 1)
return 1;
}
fragments(rows, array);
return 0;
}
I was trying to solve a question. And it said that I can get user input with some numbers into an array and display that pair of those number which’s difference is highest. I have solved the differences of all the rows line by line but now I'm unable to print that specific row which has the highest difference among other rows.
Here's my piece of code -
#include<stdio.h>
#include<limits>
int main()
{
int array1[10][10];
int num, row, column;
printf("How many rows and columns are needed: ");
scanf("%d %d", &row, &column);
printf("\nHow many numbers you want to enter: ");
scanf("%d", &num);
printf("\nEnter %d numbers: ", num);
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
{
scanf("%d", &array1[i][j]);
}
printf("\n");
}
for(int i=0; i<row; i++)
{
int largeA = array1[i][0];
int smallA = array1[i][0];
int highEleInArray = array1[0][0];
for(int j=0; j<column; j++)
{
if(array1[i][j] > largeA)
{
largeA = array1[i][j];
}
if(array1[i][j] < smallA)
{
smallA = array1[i][j];
}
}
unsigned diff = 0u + largeA - smallA;
printf("%d\n", diff);
int flag = 0;
for(int j=0; j<i; j++)
{
flag = 1;
//check if there's any similar column
for(int k=0; k<=column; k++)
{
if(array1[i][k] != array1[i][k])
{
flag = 0;
}
if(flag == 1)
{
break;
}
}
//if none of the row is similar
if(flag == 0)
{
for(int j=0; j<column; j++)
{
if(array1[i][j] > highEleInArray)
{
if(highEleInArray == diff)
{
highEleInArray = array1[i][j];
printf("\n%d\n", highEleInArray);
}
}
}
}
}
}
return 0;
}
I was expecting this output -
Sample Input
20 27 28 22
12 22 12 20
22 24 25 33
33 30 30 29
Sample Output
8
10
11
4
22 24 25 33 has the highest difference which is 11.
All you need to do is remember the index of the highest row, as well as the difference, like this:
unsigned highdiff = 0;
int highrow = 0;
...
if (diff > highdiff)) {
highdiff = diff;
highrow = i;
}
...
for(int j=0; j<column; j++) {
printf("%d ", array1[highrow][j]);
}
printf("has the highest difference which is %u\n", highdiff);
I'm asked to find the highest frequency from an array of elements and all elements with said frequency. My code seem to work just fine but it seems to have a mistake somewhere when i submit it. Can anyone help me find the error?
Format Input:
The first line contains an integer T stating the number of test cases. For each test case, the first line contains a single integer N which indicate the number of element in the array. The next line contains N integers Xi (1≤i≤N) which indicate ith element in the array.
Format Output:
Consists of T lines where each line has the format “Case #X: Y ”, where X is the test case number starting at 1 and Y is the highest frequency. Next line contains all elements which have that frequency sorted in ascending order.
Constraints:
1 ≤ T ≤ 20 | 2 ≤ N ≤ 20.000 | 1 ≤ Xi ≤ 2 × 10^5
Sample Input:
3
8
1 1 2 2 3 4 5 5
8
5 5 4 3 2 2 1 1
4
1 1 1 3
Sample Output:
Case #1: 2
1 2 5
Case #2: 2
1 2 5
Case #3: 3
1
Here is my code:
#include <stdio.h>
int main() {
int T, N[20];
scanf("%d", &T); getchar();
int A[T][20000];
for (int i = 0; i<T; i++) {
scanf("%d", &N[i]); getchar();
for (int j = 0; j<N[i]; j++) {
scanf("%d", &A[i][j]); getchar();
}
int X = 0;
for (int j = 0; j<N[i]; j++) {
for (int k = j + 1; k<N[i]; k++) {
if (A[i][k]<A[i][j]) {
X = A[i][j];
A[i][j] = A[i][k];
A[i][k] = X;
}
}
}
}
int f[20000];
for (int i = 0; i<T; i++) {
int c = 0, mc = 0;
for (int j = 0; j<N[i]; j++) {
c = 1;
if(A[i][j] != -1) {
for (int k = j+1; k<N[i]; k++) {
if (A[i][j] == A[i][k]) {
c++;
A[i][k] = -1;
}
}
f[j]=c;
}
if (c>mc) {
mc = c;
}
}
printf("Case #%d: %d\n", i+1, mc);
for (int j = 0; j<N[i]; j++) {
if (A[i][j] != -1) {
if (f[j] == mc) {
printf ("%d", A[i][j]);
if (j<N[i]-1) {
printf(" ");
}
}
}
}
printf("\n");
}
return 0;
}
EDIT
So I made another code where instead of inputting all arrays at once and outputting everything at once, this code outputs the frequency and elements after i input the first arrays of numbers. But it seems like the code still have problems and i can't find where... P.s I'm pretty new to this, so i apologise for the lack of efficiency of my codes.
NEW CODE
#include <stdio.h>
int main() {
int T, N;
scanf("%d", &T); getchar();
int A[20000];
for (int i = 0; i<T; i++) {
scanf("%d", &N); getchar();
for (int j = 0; j<N; j++) {
scanf("%d", &A[j]); getchar();
}
int X;
for (int j = 0; j<N; j++) {
for (int k = j + 1; k<N; k++) {
if (A[k]<A[j]) {
X = A[j];
A[j] = A[k];
A[k] = X;
}
}
}
int f[N], c = 0, mc = 0;
for (int j = 0; j<N; j++) {
c = 1;
if(A[j] != -1) {
for (int k = j+1; k<N; k++) {
if (A[j] == A[k]) {
c++;
A[k] = -1;
}
}
f[j]=c;
if (c>mc) {
mc = c;
}
}
}
printf("Case #%d: %d\n", i+1, mc);
for (int j = 0; j<N; j++) {
if (A[j] != -1) {
if (f[j] == mc) {
printf ("%d", A[j]);
if (j<N-1) {
printf(" ");
}
}
}
}
printf("\n");
}
return 0;
}
It took me a couple of days but i finally got how to do this. Apparently, it was not as complicated as i thought... here is the working code. Thanks to everyone who helped :)
#include <stdio.h>
int main() {
int T, N;
scanf("%d", &T);
for (int i = 0; i<T; i++) {
scanf("%d", &N); getchar();
//INPUT elements and counting frequncy for each element
int f[200001] = {0}, E = 0;
for (int j = 0; j<N; j++) {
scanf("%d", &E); getchar();
f[E]++;
}
//find max frequency and how many elements with max frequency
int max = 0, c = 0;
for (int j = 1; j<200001; j++) {
if (f[j] == max) {
c ++;
}
if (f[j]>max) {
max = f[j];
c = 1;
}
}
//OUTPUT result
printf("Case #%d: %d\n", i+1, max);
int counter = 0;
for (int j = 1; j<200001; j++) {
if (f[j] == max) {
counter ++;
if (counter<c){
printf("%d ", j);
} else {
printf("%d\n", j);
}
}
}
}
return 0;
}
I am practicing my C program currently and I want to calculate two 2D matrix.
But I get confused when I use printf() to check these arrays. They break the program.
Code as below:
int matric_multi_main()
{
int m1_row = 0;
int m1_col = 0;
int m2_row = 0;
int m2_col = 0;
int **matric1;
int **matric2;
printf("Please enter Matric 1 row: ");
scanf("%d", &m1_row);
printf("Please enter Matric 1 column: ");
scanf("%d", &m1_col);
printf("Please enter Matric 2 row: ");
scanf("%d", &m2_row);
printf("Please enter Matric 2 column: ");
scanf("%d", &m2_col);
if (m1_col != m2_row)
{
printf("Error matric size!!!\n");
return 0;
}
/* Allovate memory for matric 1 */
matric1 = malloc(m1_row * sizeof(int *));
for (int i = 0; i < m1_row; i++)
{
matric1[i] = malloc(m1_col * sizeof(int));
}
/* Allovate memory for matric 2 */
matric2 = malloc(m2_row * sizeof(int *));
for (int i = 0; i < m2_row; i++)
{
matric2[i] = malloc(m2_col * sizeof(int));
}
/* Init matric 1 */
for (int i = 0; i < m1_row; i++)
{
for (int j = 0; j < m1_col; j++)
{
printf("[%d, %d] = ", i, j);
scanf("%d", matric1+i*m1_row+j);
}
}
/* Init matric 2 */
for (int i = 0; i < m2_row; i++)
{
for (int j = 0; j < m2_col; j++)
{
printf("[%d, %d] = ", i, j);
scanf("%d", matric2+i*m2_row+j);
}
}
for (int i = 0; i < m1_row; i++)
{
printf("[");
for (int j = 0; j < m1_col; j++)
{
//printf("%d ", *(matric1 + i*m1_row + j)); <- correctly
//printf("%d ", &matric1[i][j]); <-- display the data with wrong order
printf("%d ", matric1[j][i]); <- break the program
}
printf("]\n");
}
for (int i = 0; i < m2_row; i++)
{
printf("[");
for (int j = 0; j < m2_col; j++)
{
//printf("%d ", *(matric2 + i*m2_row + j));
printf("%d ", matric2[i][j]);
}
printf("]\n");
}
}
I think I can use matric1[i][j] to get the correct data directly.
But the example on the website always use array[][] directly.
I can understand the different between my program and example.