I know this has been posted before but I tried to do on my way
I tried to code but which is showing erroneous result! obviously there is error in my logic..can anyone please explain me where am I having error?
here we are assuming that arrays are sorted in descending order!
int kthlargestsum(int a[], int b[],int k)
{
int aIndex=0;
int bIndex=0;
int sum=0;
int i;
for(i=0;i<k;++i)
{
if(a[aIndex]>b[bIndex])
{
sum+=a[aIndex];
++aIndex;
}
else
{
sum+=a[bIndex];
++bIndex;
}
}
printf("the output is %d",sum);
}
main()
{
int a[]={10,9,6,4,2};
int b[]={11,9,7,1};
int k;
printf("enter the value of k \n");
scanf("%d",&k);
kthlargestsum(a,b,k);
}
First off, a[bIndex] seems suspect to me. Perhaps you want b[bIndex]?
For anything else, you'll have to be more clear on what the problem you're trying to solve actually is (examples are always nice:). What you code appears to do is to add up k entries from the two arrays. This is now how I would interpret "Find kth largest of sum of elements in 2 array".
Related
I've written a function to rotate an array,
I've checked and it works when code is in one block I.E. everything
is coded under main(), but when I divide the code so that the rotation is done under a different function I can't get it to work (it truncates instead of rotating).
I'm pretty sure it's something to do with the array pointer.
sorry complete noob
please help:
#include<stdio.h>
void rotate(int *arr,int length);
int main()
{
// this code creates an array via input
int length;
int i;
int num;
printf("enter length of array\n");
scanf("%d",&length);
int arr[length];
for (i=0;i<length;i++) {
printf("enter number\n");
scanf("%d",&num);
arr[i]=num;
}
// just prints original
for(i=0;i<length;i++){
printf("original arr[%d]=%d\n",i,arr[i]);
}
//runs rotate function
rotate(arr,length);
return 0;
}
//the rotate function inputs rotation amount and uses nested for loop to
execute
void rotate(int *arr,int length)
{
int n;
printf("by how many do you want to rotate array?");
scanf("%d",&n);
int i;
int j;
int temp;
for (j=0;j<n;j++)
{
temp=arr[0];
for (i=0;i<length-1;i++)
{
arr[i]=arr[i+1];
}
arr[length-1]=temp;
printf("rotated arr[%d] = %d\n",i,arr[i]);
}
}
my output looks like this:
enter length of array
5
enter number
1
enter number
2
enter number
3
enter number
4
enter number
5
original arr[0]=1
original arr[1]=2
original arr[2]=3
original arr[3]=4
original arr[4]=5
by how many do you want to rotate array?
3
rotated arr[4] = 1
rotated arr[4] = 2
rotated arr[4] = 3
RUN FINISHED; exit value 0; real time: 9s; user: 0ms; system: 0ms
In C you need to declare the function before "main" function, or do the declaration and definition both above the main function. Also do share your error message, for help.
Also, in C language you can't really create dynamic arrays like that( i.e. taking an integer value and then defining the size of array using it "int array[integer] " is wrong of doing it, if the value of integer is being given during runtime)
Read http://www.mathcs.emory.edu/~cheung/Courses/255/Syllabus/2-C-adv-data/dyn-array.html , or any other tutorial about dynamic arrays in C and how to use malloc and calloc.
According to me the problem is in your last print statement in rotate method which is inside the loop.
you must loop through the whole array again to print the rotated array.
like this.
void rotate(int *arr,int length)
{
int n;
printf("by how many do you want to rotate array?");
scanf("%d",&n);
int i;
int j;
int temp;
for (j=0;j<n;j++)
{
temp=arr[0];
for (i=0;i<length-1;i++)
{
arr[i]=arr[i+1];
}
arr[length-1]=temp;
}
for(i=0;i<length;i++){
printf("original arr[%d]=%d\n",i,arr[i]);
}
}
this will work.
And also, Always define functions at the top in c.
it asks two questions at once,why?
here is my code:
#include <stdio.h>
int main()
{
int i,j;
int n;
int adjmatrix[n][n];
char ans;
printf("How many vertices?");
scanf("%d",&n);
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
printf("Are vertex %d and %d adjacent?",i,j);
scanf("%c",&ans);
if (ans=='Y' || ans=='y')
{
adjmatrix[i][j]=1;
}
else adjmatrix[i][j]=0;
}
}
return 0;
}
Thanks in advanceļ¼
You're right. The output is weird.
Your problem is scanf. Use scanf("%2c", ans);. Helped me.
But even then, i had the problem that the loops are not iterated
correctly.
Explanation:
The reason was the matrix. The matrix is not allocating any memory because its size if variable.
Therefore by setting an entry of the matrix to 1, this influences the variable j.
Solution:
You need to solve the problem to dynamically allocate memory.
Have a look at:
https://www.eskimo.com/~scs/cclass/int/sx9b.html
https://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/
I'm trying to write a program which includes an array that filled by user and find a value in it which specified by user then print if it found and count of that number in array.But it works only for first element of array.My code is below:
`void searchh(int arr[],int search,int number,int counter);
int main()
{
int number,search,i;
int counter=0;
printf("How many numbers will you enter?");
scanf("%d",&number);
int array[number];
for(i=0;i<number;i++){
printf("Please enter the %d. element of the array:",i+1);
scanf("%d",&array[i]);
}
printf("Please enter the number that you're looking for:");
scanf("%d",&search);
searchh(array,search,number,counter);
return 0;
}
void searchh(int arr[],int search,int number,int counter){
int i,c;
int key=search;
int num=number;
counter=0;
int arrsize=(int)(sizeof(arr)/sizeof(int));
int arrayent[(int)(sizeof(num)/sizeof(int))];
for(i=0;i<arrsize;i++)
{
if(arr[i]==key)
{
arrayent[counter]=i;
counter++;
}
}
printf("The number that you're looking for which is %d is found %d times.\nLocations:",key,counter);
if(counter>0){
for(c=0;c<sizeof(arrayent)/sizeof(int);c++){
printf("%d\n",arrayent[c]);
}
}
else
printf("Number doesn't exist!!");
}`
And Outputs:
Thanks for your helps.
int arrsize=(int)(sizeof(arr)/sizeof(int));
This already doesn't do what you think it does. sizeof(arr) - could be 4 if size of pointer is 4 bytes. In other words you can't check array size like that inside function, arr decays to pointer of first element of array. Hence sizeof(arr) will return size of pointer which could be 4 or 8. You need to pass the number of elements of the array to the function as parameter - which is number in your case.
This:
int arrayent[(int)(sizeof(num)/sizeof(int))];
is also strange. num is int. sizeof(num) and sizeof(int) will be same - and division will give you 1.
IMO these two lines
int arrsize=(int)(sizeof(arr)/sizeof(int));
int arrayent[(int)(sizeof(num)/sizeof(int))];
should just go as
int arrsize = number;
int arrayent[number];
PS. Also try to use a debugger to help you with some kind of issues.
I am a complete beginner in C and I am practicing passing arrays into functions. I wrote a program to take a two dimensional array as input and find sum of the individual columns.
And when I compiled the program I got no errors, but once I run it, I get a dialogue box saying "untitled5.exe stopped working" where untitled5 is the file name.
I got this error quite a few times. I have used both dev c++ and codeblocks to compile my program, so what is the reason for this? Is this a problem with my code or with my compiler or with my laptop?
#include<stdio.h>
void summation (int arr[][5], int size);
int main()
{
int n,arr[n][5],sum,i,j;
printf("enter the number of rows");
scanf("%d",&n);
for (i=0;i<n;i++)
{
for (j=0;j<5;j++)
{
printf("%d,%d th element is",i,j);
scanf("%d",&arr[i][j]);
}
}
summation (arr,5);
return 0;
}
void summation (int arr[][5], int size)
{
int i,j,s=0;
for(j=0;j<5;j++)
{
for (i=0;i<5;i++)
{
s=s+arr[i][j];
}
printf("%d",s);
}
}
In main() you are using i to index the first dimension of the array. In summation() you are using i to index the second dimension of the array. I think that you are going beyond the end of the first dimension inside summation() when main() does not fill up that much of the array (e.g., when you enter 2 for the number of rows).
I think you want
summation (arr,5);
And, inside summation():
for (i=0;i<size;i++)
{
s=s+arr[i][j];
}
#include<stdio.h>
void summation (int arr[][5], int size, int rows);
int main()
{
int n, sum, i, j;
printf("enter the number of rows");
scanf("%d",&n);
int arr[n][5];
for (i=0;i<n;i++)
{
for (j=0;j<5;j++)
{
printf("%d,%d th element is",i,j);
scanf("%d",&arr[i][j]);
}
}
summation (arr, 5, n);
return 0;
}
void summation (int arr[][5], int size, int rows)
{
int i,j,s=0;
for(i=0;i<rows;j++)
{
for (j=0;i<size;i++)
{
s=s+arr[i][j];
}
}
printf("%d",s);
}
So first off I moved your array declaration to after you have initialized n and made it equal to something.
Then your next problem was you were probably going out of bounds in your summation function. You always have 5 columns in your 2darray, but you can have a different amount of rows. Pass the amount of rows, n, into the function summation to make sure you don't go out of bounds.
I am trying to solve this question, but on codechef its showing wrong answer,link to the queston is http://www.codechef.com/problems/STATUES/ .On system ,its showing correct answer, after trying a lot, i couldnt find the bug My code is,
#include<stdio.h>
//#include<conio.h>
int main()
{
int a[150];
int n;
int i;
int sum;
int avg;
int count=0;
scanf("%d",&n);
int ans;
int diff;
while(n!=0)
{
sum=0;
ans=0;
count++;
for(i=0;i<n;i++)
{ scanf("%d",&a[i]);
sum=sum+a[i];
}
avg=sum/n;
for(i=0;i<n;i++)
{ diff=avg-a[i];
if(diff>0)
ans=ans+diff;
}
printf("Set#%d\nThe minimum number of moves is %d.\n",count,ans);
scanf("%d",&n);
}
return 0;
}
Read the condition in the question:
Output a blank line after each test case.
Be careful about the case and punctuation in the above strings. Not adhering to the output format strictly will lead to the Wrong Answer verdict.
There is no blank line after your each output. Also one more for loop is needed to met the condition for exact output format. Add this snippet after while.
for(i=0;i<n;i++)
{
printf("\nSet #%d\nThe minimum number of moves is %d.\n",i,ans);
}
Another problem is
int a[150];
a should not exceed from 50. Change it to
int a[50];
But I woul advice you to use variable length array in this case to save memory.