I have the following code which is driving me nuts. I am trying to input values into a 2-dimensional array.
I do not really know how I can insert a value in to specific location in the array. For example I want to enter the values in a pyramid fashion. I should be able to add one more value in each level.
a[0][0] ===> 1
a[1][0] & [1][1] &[1][2] ===> 2 3
a[2][0] & [2][0] & [2][2] ===> 4 5 6 etc.
I also want to be able to store the greatest integer In each level so that I can sum all the large integers in each level.
So far with the following code, I am unable to figure out how to insert the value and I also cannot sum the greatest values in each level.
for(i = 0; i < 2; i++){
for(j = 0; j <= 4; j++){
printf("Enter the values in to the array");
scanf("%d",&arr[i][j]);
}
}
for(i = 0; i < 2; i++){
for(j = 0; j <= 4; j++){
if(arr[i][j] > arr[i][j+1]){
holder = arr[i][j];
}else{
holder = arr[i][j+1];
}
}
sum = sum+holder;
}
printf("%d\n\n",sum);
To enter the values in a pyramid fashion & calculate the sum of the largest numbers of each row, try the code below:
int arr[5][10];
int i,j;
int holder=0;
int sum = 0;
for (i=0;i<5;i++)
{
for(j=0; j<=i;j++)
{
printf("Enter your input: \n");
scanf("%d", &arr[i][j]);
if(arr[i][j] > holder)
{
holder = arr[i][j];
}
}
printf("Largest Value in Row %d is %d\n", i, holder);
sum = sum + holder;
holder = 0;
}
printf(" Sum = %d\n", sum);
Try the code below for dynamically deciding the array size and assigning the values:-
int **arr;
int *greatest_each_lavel;
int n, i, j, temp;
printf ("enter the level.\n");
scanf ("%d", &n);
if (n<1)
{
printf ("Wrong input.\n");
exit (1);
}
greatest_each_level = (int *) malloc (n * sizeof (int));
arr = (int **) malloc (n * sizeof (int *));
for (i=1; i<=n; i++)
{
arr[i] = (int *) malloc (i * sizeof (int));
}
// Populate the array members.
for (i=0; i<n; i++)
{
temp = 0;
for (j=0; j<=i; j++)
{
printf ("Enter the number ");
scanf ("%d", &arr[i][j]);
if (arr[i][j] > k)
{
temp = arr[i][j];
}
}
greatest_each_level[i] = temp;
}
// To get the sum of greatest number at each level
int sum = 0;
for (i = 0; i < n; i++)
sum = sum + greatest_each_level[i];
printf ("Sum is : %d\n", sum);
// When all task are done, dont forget to free the memory.
for (i=0; i<n; i++)
{
free (arr[i]);
}
free (arr);
free (greatest_each_level);
greatest_each_level = NULL;
arr = NULL;
Related
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;
}
Im taking a C programming beginning course, and have a problem with a for loop.
I want to make two double arrays. One that takes input from user, and one that sums the inputs.
I then want to output the two arrays. One that shows the input, and one that shows the sums og the input in every cell.
The problem comes when I try to show the sum for every "cell" in the array.
The output just becomes the same as the input.
I can solve it with:
// print all the numbers in the second array
for (i = 0; i < n; i++) {
sum = sum + a[i];
printf("%lf ", sum);
but then the assignment wouldn't be solved. Hope you can educate me.
#include <stdio.h> #include <stdlib.h>
int main(void) {
int n; //numbers of cells
int i; //The numbers in the cells
printf("How many cells in the array would you like? \n");
scanf("%d", &n);
double a[n], sum=0; // declare an array and a loop variable.
double b[n], sumo=0;
printf("Enter the numbers:\n");
for (i = 0; i < n; i++) { // read each number from the user
scanf("%lf", &a[i]);
}
printf("The results, my lord:\n");
// print all the numbers in the first array
for (i = 0; i < n; i++) {
printf("%lf ", a[i]);
}
printf("\n"); //THIS IS WHERE THE PROBLEM STARTS
// print all the numbers in the second array
for (i = 0; i < n; i++) {
b[i] = b[i] + a[i];
printf("%lf ", b[i]);
}
return 0;
}
For starters the array b is not initialized
double b[n], sumo=0;
So this statement
b[i] = b[i] + a[i];
invokes undefined behavior.
It seems what you need is the following for loop
for (i = 0; i < n; i++) {
b[i] = a[i];
if ( i != 0 ) b[i] += a[i-1];
printf("%lf ", b[i]);
}
You can calculate the sum (aka the b array) when you read the user data:
for (i = 0; i < n; i++)
{
scanf("%lf", &a[i]);
if (i == 0)
{
b[i] = a[i];
}
else
{
b[i] = b[i-1] + a[i];
}
}
or do it like:
scanf("%lf", &a[0]);
b[0] = a[0];
for (i = 1; i < n; i++)
{
scanf("%lf", &a[i]);
b[i] = b[i-1] + a[i];
}
or do it like:
sum = 0;
for (i = 0; i < n; i++)
{
scanf("%lf", &a[i]);
sum += a[i];
b[i] = sum;
}
The last part is how I solved the problem. I found the issue that. I didn't update the "sum" for ever loop. It adds a new value in b[I] this way. :-)
double a[n], sum=0; // declare an array and a loop variable.
double b[n];
printf("Enter the numbers:\n");
for (i = 0; i < n; i++) { // read each number from the user
scanf("%lf", &a[i]);
}
printf("The results, my lord:\n");
// print all the numbers in the first array
for (i = 0; i < n; i++) {
printf("%.2lf ", a[i]);
}
printf("\n");
// print all the numbers in the second array
for (i = 0; i < n; i++) {
sum = sum + a[i];
b[i] = sum;
printf("%.2lf ", b[i]);
I have been trying to create an NxN square matrix (the size of the square matrix is determined by the user) but seem to have run into an issue. The displayed matrix is actually a symmetric matrix which is not what I am trying to as I want to be able to have each value hold a unique integer. Any help is greatly appreciated. Here's my code:
#include <stdio.h>
int main()
{
int i;
int j;
int size = 1;
int array [size][size];
printf ("Enter the size you would like for the nxn matrix: ");
scanf ("%d", &size);
printf ("Now enter elements into the matrix. \n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf ("[%d][%d] = ", i, j);
scanf ("%d", &array [i][j]);
}
}
printf ("------------------------\n\n");
printf ("the matrix is: \n\n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf (" %d ", array [i][j]);
}
printf ("\n");
}
return (0);
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
// Create variables
int i, j, size;
// Create 2D array
printf ("Enter the size you would like for the nxn matrix: ");
scanf ("%d", &size);
int *array = (int *)malloc(size * size * sizeof(int));
// Fill 2D array with values
printf ("Now enter elements into the matrix. \n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf ("[%d][%d] = ", i, j);
scanf ("%d", &array[i * size + j]);
}
}
printf ("------------------------\n\n");
// Display values of 2D array
printf ("the matrix is: \n\n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf ("%d", array[i * size + j]);
}
printf ("\n");
}
free(array);
return (0);
}
This question already has answers here:
Getting a stack overflow exception when declaring a large array
(8 answers)
Closed 4 years ago.
Trying to multiply two matrices of the dimension 1000*1000 . However, trying to do so causes Segmentation fault. what is possibly causing this and how to resolve it?
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
clock_t t;
t = clock();
long int a[1000][1000], b[1000][1000], result[1000][1000], r1=1000, c1=1000, r2=1000, c2=1000, i, j, k;
// Column of first matrix should be equal to column of second matrix and
/* while (c1 != r2)
{
printf("Error! column of first matrix not equal to row of second.\n\n");
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
}
*/
// Storing elements of first matrix.
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
a[i][j]=rand()%20;
}
// Storing elements of second matrix.
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
b[i][j]=rand()%20;
}
// Initializing all elements of result matrix to 0
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
result[i][j] = 0;
}
// Multiplying matrices a and b and
// storing result in result matrix
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
result[i][j]+=a[i][k]*b[k][j];
}
// Displaying the result
printf("\nOutput Matrix:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%ld ", result[i][j]);
if(j == c2-1)
printf("\n\n");
}
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
printf("\n \nfunction took %f seconds to execute \n", time_taken);
return 0;
}
You need to use dynamic memory allocation in the case of there large memory allocations. Stack memory cannot handle these large memory requirements.
You can solve this using Dynamic memory allocation. Try :-
int (*a)[r1][c1] = malloc(sizeof *a);
int (*b)[r2][c2] = malloc(sizeof *b);
int (*result)[r1][c2] = malloc(sizeof *result);
And access elements using :-
(*result)[i][j] ;
(*a)[i][k] ;
(*b)[k][j] ;
Try this code :-
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
clock_t t;
t = clock();
int r1 = 1000, c1 = 1000, r2 = 1000, c2 = 1000, i, j, k;
// Dynamic allocation.
int(*a)[r1][c1] = malloc(sizeof *a);
int(*b)[r2][c2] = malloc(sizeof *b);
int(*result)[r1][c2] = malloc(sizeof *result);
// Storing elements of first matrix.
printf("\nEnter elements of matrix 1:\n");
for (i = 0; i < r1; ++i)
{
for (j = 0; j < c1; ++j)
{
(*a)[i][j] = rand() % 20;
}
}
// Storing elements of second matrix.
printf("\nEnter elements of matrix 2:\n");
for (i = 0; i < r2; ++i)
{
for (j = 0; j < c2; ++j)
{
(*b)[i][j] = rand() % 20;
}
}
// Initializing all elements of result matrix to 0
for (i = 0; i < r1; ++i)
{
for (j = 0; j < c2; ++j)
{
(*result)[i][j] = 0;
}
}
// Multiplying matrices a and b and
// storing result in result matrix
for (i = 0; i < r1; ++i)
for (j = 0; j < c2; ++j)
for (k = 0; k < c1; ++k)
{
(*result)[i][j] += (*a)[i][k] * (*b)[k][j];
}
// Displaying the result
printf("\nOutput Matrix:\n");
for (i = 0; i < r1; ++i)
for (j = 0; j < c2; ++j)
{
printf("%d ", (*result)[i][j]);
if (j == c2 - 1)
printf("\n\n");
}
t = clock() - t;
double time_taken = ((double)t) / CLOCKS_PER_SEC; // in seconds
printf("\n \nfunction took %f seconds to execute \n", time_taken);
free(a);
free(b);
free(result);
return 0;
}
Output :-
.......................................................................
91717 92211 96529 90328 89167 88774 90433 88320 93834 89054 92225 92226 89919 88005 90772 90436 89091 92446 88477 94143 95777 88805 88487 89082 92528 88899 93436 90423 88637 90254 91569 87516 89079 91309 93554 86422 90069 91096 86981 95437 92805 88638 89828 88568 89607 88025 91700 88144 90401 91147 88284 92998 90959 85520 92640 92247 95616 90006 87248 89726 91751 90077 90543 91489 92399 90828 89026 92866 91548 87131 88450 93247 87748 90734 90228 91972 93300 92444 91592 85842 91167 89554 91144 90536 91256 89646 92815 91476 91863 94836 95462 87122 91735 96059 91312 90480 93306
function took 6.060788 seconds to execute
Note : Full output can't be included since too large.
Don't forget to free allocated memory.
First of all do not allocate such a huge amounts of memory as the local function variables. Their storage location (usually the stack) is relatively small comparing to the size of the heap You just run out of the automatic variables storage.. Instead allocate it dynamically
int *a = malloc(r1 * c1 * sizeof(*a));
int *b = malloc(r2 * c2 * sizeof(*b));
int *result = malloc(r2 * c2 * sizeof(*result));
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
a[i * c1 + j]=rand()%20;
}
....
You can have another allocation strategy to have more natural indexing.
I am trying to copy elements from two arrays into a third.
I can't understand why it doesn't work.
I made sure that the two arrays are filled properly, but for some reason, the actual copying doesn't work- when I print the elements of the arr3, I get some random numbers.
#include <stdio.h>
int main()
{
int arr1[10], arr2[10], arr3[20];
int i, n;
printf("Enter a number of elements to be stored in each array (up to 10): ");
scanf("%d", &n);
printf("Enter the %d elements to the first array:\n", n);
for (i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr1[i]);
}
printf("Enter the %d elements to the second array:\n", n);
for (i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr2[i]);
}
/*
// A test to make sure first 2 array are filled by the user- works
for(i = 0; i < n; i++)
printf("%d ", arr1[i]);
for(i = 0; i < n; i++)
printf("%d ", arr2[i]);
*/
// something wrong here, the elements are not coppied to the third array
for(i = 0; i < n; i++);
arr3[i] = arr1[i];
for(i = n; i < 2 * n; i++)
arr3[i] = arr2[i];
for(i = 0; i < 2 * n; i++)
printf("%d\n", arr3[i]);
return(0);
}
You're reading past the end of arr2, try this;
for (i = 0; i < n; i++)
arr3[i] = arr1[i];
for (i = 0; i < n; i++)
arr3[n+i] = arr2[i];
That's because your code is reading arr2[10],arr2[11] ..... arr2[19] (if n=10 ), all these values do not exist because arr2 only has 10 values. you can use this.
for (i=0; i<n; i++)
arr3[n+i]=arr2[i];
or
for (i=n; i<n*2; i++)
arr3[i]=arr2[i-n];