This is the code I wrote for finding the maximum sum subarray using Kadane's algorithm.
Code:
#include <stdio.h>
int maxSum(int a[],int size)
{
int ans=0; //stores the final sum
int sum=0; //stores the sum of the elements in the empty array
int i;
for(i=0;i<size;i++)
{
sum=sum+a[i];
if(sum<0)
{
sum=0;
}
if(ans<sum)
{
ans=sum;
}
}
return ans;
}
void main(){
int j,size,t,total; //size=size of the array,t=number of test cases
scanf("%d\n",&t);
while(t-->0)
{
int a[size];
for(j=0;j<size;j++)
{
scanf("%d ",&a[j]);
}
total=maxSum(a,size);
printf("\n%d",total);
}
}
I keep getting the wrong output:
For Input:
2 //test cases
3 //case 1
1 2 3
4 //case 2
-1 -2 -3 -4
Your Output is:
0 //it should be 6
0 //it should be -1
The only error is you haven't initialized size before using it to define the size of the array a - otherwise program is fine.
MAJOR--> It seems you are coding it on Turbo (as you have used void main() rather than int main(void)) which is an outdated compiler - shift to GCC or CLANG.
I have to pass two arrays ( A and B with dimensions of 5X4) to two functions called FUNCTION_1 and FUNCTION_2. Both of arrays columns and rows should be passed as POINTERS. FUNCTION_1 will take each element of A array and calculate the sum of prime factors of each element located in A( (with the help of another function called sumPrime), then it will store these sums in array B. FUNCTION_2 has to print both of A and B arrays. ( The normal numbers array, and the prime factors sums array). There are some additions in the program which are not important now but I am going to show them too in respect of clearness.
#include <stdio.h>
#include <stdlib.h>
#define ROW 5
#define COL 4
# include <math.h>
int sumPrime(int number){
int factor = 2;
int sum=0;
while(1!=number){
if(number%factor==0){
number /= factor;
sum+=factor;
factor = 2;
continue;
}
factor++;
}
return sum;
}
int FUNCTION_1(int *a[][20],int *b[][20],int row, int col){
int c,d;
for(c=0;c<row;c++){
for(d=0;d<col;d++){
b[c][d]=sumPrime(a[c][d]);
return b[c][d];
}
}
}
void FUNCTION_2(int *x[][20],int *y[][20],int rows, int cols){
printf(" \n A matrix is :\n");
int e,f;
for(e=0;e<rows;e++){
for(f=0;f<cols;f++){
printf("A[%d][%d] is %d\n",e,f,x[e][f]);
}
}
printf("\n B matrix is:\n");
for(e=0;e<rows;e++){
for(f=0;f<cols;f++){
printf("A[%d][%d] is %d\n",e,f,FUNCTION_1(x,rows,cols,y,rows,cols));
}
}
}
int main(){
int A[ROW][COL]={0};
int B[ROW][COL]={0};
int x=1;
int i;
int j;
for(i=0;i<ROW;i++){
for(j=0;j<COL;j++){
A[i][j]=x;
x=x+2;
}
}
printf("%d",A[0][0]);
return 0;
}
When trying to perform FUNCTION_1 or FUNCTION_2 I get many errors or even when defining the functions. There is no problem with defining A matrix or prime function! HELP!!
Your definition for a matrix is wrong ..
Use :
func(int* a[20] , int cols)
in this case you will have 20 rows ,
or
func(int a[][20] , int rows)
And define some macro for the columns if it is fixed value .
Two-dimensional integer array (5x5) with only 0 and 1 values.
There are:
8 neighbours for any element in the middle of the matrix.
5 neighbours for the elements on the border (row=0, column=0, row=4 or column=4) of the matrix
3 neighbours for the elements on the corner of the matrix.
Block contains connected 1.
In this homework, you will write a complete C program to create a 2-D (5x5) array and fill the array with random numbers (0 or 1). Program will find the number of blocks in the matrix (5x5).
Here is my question, I fill array with numbers but I can't find how to connect 1s as block.
This is my code:
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
void find(int arr[][5],int size);
int main()
{
int arr[5][5];
int i,j;
srand (time(NULL));
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
arr[i][j]=rand()%2;
}
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
find(arr,5);
}
void find(int arr[][5],int size)
{
int i,j,count=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
}
}
}
a 'brute' force algorithm would use the facts as you laid out.
I.E. 4 checks for the corners 12 checks for the edges, 9 checks for the interior blocks
and example corner check:
int countOfBlocks = 0;
if( arr[0][0] && arr[0][1] && arr[1][0] && arr[1][1] ) countOfBlocks++;
...
The code would then take 25 lines of 'if's, similar to the above
I am here to get some advice on how to continue my program. It is a homework assignment and the idea is to have another method called int is_sorted(int array[], int length);
With these pre and post conditions.
Precondition: array will be an array of integers of length length.
Postcondition: returns true if the array is in sorted(nondecreasing) order, or false otherwise.
So far I have been able to put together the user input array and how long it should be.
#include <stdio.h>
#include <math.h>
int is_sorted(int array[], int lenght);
int is_sorted(int array[], int lenght)
{
int swap;
int smallest;
int index = 0;
scanf("%d", &lenght);
int list[lenght];
int i;
for (i = 0; i < lenght; i++)
{
scanf("%d", &list[i]);
}
return 0;
}
int main()
{
}
How would I go about asking for a user input to swap two elements at a time within the given array?
The final product should look similar to this:
Sample Run: User input in bold
4 <- The length that the array should be.
1 1 1 2 <- user input these 4 numbers.
WHAT IS THE NEXT SWAP? 2 3
EVAN HAS UNSORTED THE ARRAY.
WHAT IS THE NEXT SWAP? 2 0
WHAT IS THE NEXT SWAP? 0 3
EVAN HAS SORTED THE ARRAY.
WHAT IS THE NEXT SWAP? -1 -1
STEVE WAS RIGHT!
-1 -1 end the swapping process and check if the array is sorted.
while(i != -1 && j != -1){
scanf("%d %d", &i, &j);
swap(&array[i], &array[j]);
}
and swap() looks like this:
void swap(int* a, int* b){
int c = *a;
*a = *b;
*b = c;
}
There's probably a nifty trick for swaping two variables doing some bitwise xor-ing that I don't remember about.
everyone I am trying to work on the transpose matrix
here is my code so far.
void Transpose(int mt[][10], int m, int n)
{
int c,d,temp;
for (c = 0; c < m; c++)
{
for (d = c+1; d < n; d++)
{
temp = mt[c][d];
mt[c][d]=mt[d][c];
mt[d][c] = temp;
}
}
}
void print(int pose[][10], int m, int o)
{
int i,j;
for(i = 0; i < m; i++)
{
for(j = 0; j < o; j++)
{
printf("%d\n",pose[j][i]);
}
}
}
int main()
{
/*The body of your source code will go here */
int a[4][5] = {{1,2,3,4,5},{6,7,8,9,10},{10,9,8,7,6},{5,4,3,2,1}};
printf("ARRAY: %d",a[][5]);
Transpose();
return (0);
}
Here is my function for print and transpose a matrix, but now I am trying to pass the array into the function from my main. I just wondering how I declare the array that in the main can pass to the function. Thanks
Your declaration of Transpose does not match your use of it. Since it is declared as:
void Transpose(int mt[][10], int m, int n) {...}
It should be invoked by
Transpose(a, 4, 5);
Also, what is the 10 for? And, the statement
printf("ARRAY: %d", a[][5]);
is unlikely to work.
You should choose better names for your variables. Instead of m and n, use nRows and nColumns. Use row and column instead of i and j.
You can use the following code to do it.
#include<stdio.h>
// Print Matrix
void printArr(int *A, int row,int col)
{
int i,j;
for(i=0;i<row;i++)
{
printf("\n");
for(j=0;j<col;j++)
printf("%d\t",*(A+(col*i)+j));
}
}
//Find the Transpose(TA) of Matrix(A)
void Transpose(int *A,int *TA,int row,int col)
{
int i,j;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
*(TA+(j*row)+i)=*(A+(col*i)+j);
}
// Start of Main
int main()
{
int A[4][5]={{1,2,3,4,5},{6,7,8,9,10},{10,9,8,7,6},{5,4,3,2,1}};
int *TA=malloc(sizeof(int)*4*5); // Allocate Memory for TA
int i,j;
printf("Matrix A:");
printArr(A,4,5); //Print Array A
Transpose(A,TA,4,5); //Call Transpose
printf("\nTranspose Matrix A:");
printArr(TA,5,4); // Print Array TA
return 0;
}
Output:
Matrix A:
1 2 3 4 5
6 7 8 9 10
10 9 8 7 6
5 4 3 2 1
Transpose Matrix A:
1 6 10 5
2 7 9 4
3 8 8 3
4 9 7 2
5 10 6 1
The C programming language does not remember the lengths of arrays like other programming languages do. That makes working with multidimensional arrays difficult.
To pass a multidimensional array as a parameter in C, you either have to know the dimensions of the array at compile time:
#define HEIGHT 10
#define WIDTH 13
void function_prototype(int M[HEIGHT][WIDTH]) {
access M normally with M[y][x]
}
or you have to pass a single dimensional array and the lengths as parameters:
void function_prototype(int* M, int height, int width) {
access M as M[y * width + x]
}
It's troublesome, but that's the price you pay for having extremely fast array access.