Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
#define N 10
float func ( int *arr, int n, int *count )
{
int *p,i;
float sum=0;
p=arr;
for (i=0;i<n;i++)
{
sum += *p;
p++;
}
sum /= n;
count=0;
p=arr;
for(i=0;i<n;i++) //לולאה שעוברת על כל המערך ומוסיפה כל תוכן של איברבמערך שיותר גדול מהממוצע
{
*count+= (*p>sum);
p++;
}
return sum;
}
void main()
{
int i, count=0, arr[N]={0}, n=N;
for(i=0;i<N;i++)
{
printf("Please enter your grade\n");
scanf("%d",&arr[i]);
}
for(i=0;i<N;i++)
printf("%d ",arr[i]);
printf("The average in class is: %f and the number of students that had the best grades(MORE) are: %d \n",func(arr,n,count),count);
}
I need some help on this program.
the code is above,
the program need to get from the users 10 grades and then print average and count how many grades is more then the average
Your main function is incorrectly declared. You should compile with all warnings and debug info (e.g. gcc -Wall -g if using GCC, which is probably used by your Codeblocks IDE...) then you should use the debugger (e.g. gdb). And you should test the result of scanf(3)
BTW,
count = 0; // better written as count = NULL;
is wrong: it is clearing the pointer. You probably want
*count = 0;
Also, your last printf is supposing some order of evaluation (since you expect the call to func to change count before printing count), so is undefined behavior. You need:
float avg = func(arr,n,&count);
// from http://stackoverflow.com/a/25382154/841108
printf("The average in class is: %f and the number"
" of students that had the best grades are: %d \n",
avg,count);
Plese show or tell your teacher that you got help on SO (he will find out anyway)
BTW, I can't understand why students are asking their homework on the web. They don't learn anything by doing that, and their teacher will notice anyway.
You are calculating sum and count in func, so you can't return two values at a time. so count the marks which are greater then average in main().
Try this-
#include <stdio.h>
#include <stdlib.h>
#define N 10
float func ( int *arr, int n)
{
int *p,i;
float sum=0;
p=arr;
for (i=0;i<n;i++)
{
sum += *p;
p++;
}
sum /= n;
printf("%lf \n",sum);
return sum;
}
int countfun(int *arr, float sum)
{
int i,count = 0;
for(i=0;i<N;i++)
{
if(arr[i] > sum)
count++;
}
return count;
}
void main()
{
int i, count=0, arr[N]={0}, n=N;
float sum=0;
for(i=0;i<N;i++)
{
printf("Please enter your grade\n");
scanf("%d",&arr[i]);
}
for(i=0;i<N;i++)
printf("%d ",arr[i]);
sum = func(arr,n);
count = countfun(arr,sum);
printf("The average in class is: %f and the number of students that had the best grades(MORE) are: %d \n",sum,count);
}
You can use a getchar() just after the last printf() to wait for a character.
You think that the execution window closes without printing. That is wrong. It does print and then exits before you can even see the output.
P:S - You have some problems with your code as mentioned in the rest of the answers. Fix them and then try this
Related
I'm new to C language and I could really use help with this question:
The abundancy of an integer is defined as the perfect divisors of a number (factors not including the number itself) divided by the number itself. For example, the abundance of 8 is (1+2+4)/8 = 7/8. Write a C function which takes a single integer as input and returns the abundance of that number.
This is as far as I got. I can compile it but I keep getting the incorrect answer. Please help and thanks in advance
#include <stdio.h>
int main()
{
int number, i, sum, abundancy;
sum==0;
abundancy==0;
printf("Enter an integer:");
scanf("%d", &number);
for(i=1; i<=number; i++)
{
if (i!=number)
{
if (number%i == 0)
{
sum+=i;
{
abundancy=sum/number;
printf("The abundancy is %d", abundancy);
}
}
}
}
return 0;
}
This is as far as I got on an online compiler
This is what I get when I compile it
You are doing abundancy=sum/number; inside the loop which is going to fetch you wrong results. Here is the correct version of your code with the changes:
#include <stdio.h>
int main()
{
int number, i, sum, abundancy;
sum=0; //USE ASSIGNMENT NOT EQUAL TO OPERATOR
abundancy=0; //USE ASSIGNMENT NOT EQUAL TO OPERATOR
printf("Enter an integer:");
scanf("%d", &number);
for(i=1; i<number; i++) //CHANGED
{
if (number%i == 0) //ONLY SINGLE IF NEEDED
{
sum+=i;
}
}
abundancy = sum/number;
printf("Abundancy is: %d/%d\n",sum,number); //ADDED
printf("abundancy is: %d\n",abundancy);
return 0;
}
Even then abundancy will contain floor value of sum/number - you should use float instead and %f as format specifier while printing.
INPUT:
12
OUTPUT:
abundancy is: 16/12
abundancy is: 1
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I face problems with my code when I enter 1 3 4 . although I couldn't find any error as it works perfectly with other numbers / ps. the was built as a solution to the codechef problem POTATOES
Problem summary: Write a program that inputs an integer T followed by T lines containing two space-separated positive integers. For each of these lines, output the smallest number (>1) that, when added to the sum of these two numbers, results in a sum that is a prime number.
and my code is
#include<stdio.h>
#include<math.h>
int prime(int a,int b);
int main() {
int c;
scanf("%d",&c);
int a,b,d[c];
for(a=0; a<c; a++) {
int x,y;
scanf("%d %d",&x,&y);
b=(x+y);
if(prime(x,y)-b!=0)
d[a]=prime(x,y)-b;
else d[a]=prime((x+1),y)-b;
}
for(a=0; a<c; a++)printf("%d\n",d[a]);
return 0;
}
int prime(int a,int b) {
int c,e;
for(c=2; c<(a+b); c++) {
if((a+b)%c==0) {
b++;
continue;
}
return(a+b);
}
}
Your code is wrong. In your function prime() due to the statement b++; , (a+b) is changing in (a+b)%c but the incremented c is never went back.So for bigger prime numbers your code will fail
Eg:- (89,1) (79,1) etc
Also You don't need that d[c] in your code. You don't need to store every output.When you compute one output just print it .That is ok with code-chef. Also You can divide the problem into to functions.
Try this simplified code :-
#include <stdio.h>
#include <math.h>
int prime(int n);
int make_prime(int a);
int main()
{
int c;
scanf("%d", &c);
int a, b;
for (a = 0; a < c; a++)
{
int x, y;
scanf("%d %d", &x, &y);
b = (x + y);
printf("%d\n", make_prime(b));
}
return 0;
}
int make_prime(int a)
{
int c=1;
while(prime(a+c)==0){
c++;
}
return c;
}
int prime(int n){ // simple prime function
int i,flag=1;
for (i = 2; i <= (n)/2; i++)
{
if ((n) % i == 0)
{
flag=0;
break;
}
}
return flag;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I try to find largest number using array and pointer, but program gives errors on "finding step". Can you tell me where is my mistake?
void find_two_largest(int a[], int n, int *largest)
{
int i;
for(i=0;i<n;i++)
{
printf("enter %d. value: ",i+1);
scanf("%d",&a[i]);
}
int max=a[0];
for(i=0;i<n;i++)
if(a[i]>max) max=a[i];
largest=&max;
printf("%d",*largest);
}
int main()
{
int n,i,a[100],*lar=NULL;
printf("how many elements you want to store?\t");
scanf("%d",&n);
find_two_largest(a, n, lar);
return 0;
}
The main program is not initializing *lar, the find_two_largest function is initializing max before set user values, so max could be any value in memory.
void find_two_largest(int a[], int n, int *largest)
{
int i,max;
for(i=0;i<n;i++)
{
printf("enter %d. value: ",i+1);
scanf("%d",&a[i]);
}
//initialize max after entering values
max=a[0];
for(i=0;i<n;i++)
if(a[i]>max) max=a[i];
*largest=max;
printf("%d",*largest);
}
int main()
{
int n,i,a[100],*lar,*slar;
//initialize;
*lar=INT_MIN;
printf("how many elements you want to store?\t");
scanf("%d",&n);
find_two_largest(a, n, lar);
return 0;
}
Remove * from lar definition.
like this it should be :
int n,i,a[100], lar,*slar;
and then add an & to function call :
find_two_largest(a, n, &lar);
and after that put max = a[0] after a[0] value set.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
#include <stdio.h>
#include <conio.h>
int main(void){
int n, i, a[10], sum = 0;
for(i = 0; i < 10; i++){
printf("Enter the marks of %dth student ", i + 1);
scanf("%d", a[i]);
sum = sum + a[i];
}
printf("The total sum is %d", sum);
return 0;
}
Is there an error in my program?
Everytime I run the program, after entering the marks for the first student, I get an error saying that my program has stopped working!
This happens for most of my programs where I have used arrays!
It should be
scanf("%d",&a[i]);
Pass-by-pointer, not by value. Unfortunately, some compilers cannot perform compile-time type safety checks on calls to scanf(). So basically scanf() is treating your (uninitialized value in) a[i] as a pointer, which leads to undefined behavior.
Try this:
#include <stdio.h> //stdio not Stdio
int main(void){
int n,i,a[10],sum=0;
for(i=0;i<10;i++){
printf("Enter the marks of %dth student ",i+1);
scanf("%d",&a[i]); // &a[i] not a[i]
sum=sum+a[i];
}
printf("The total sum is %d\n",sum);
return 0;
}
scanf needs a pointer, not the value.
You invoked undefined behavior by passing data having the wrong type to scanf(). You have to pass int* to scanf(), not int, for %d.
I also corrected the #includes and added input error check.
Try this:
#include<stdio.h>
int main(void){
int n,i,a[10],sum=0;
for(i=0;i<10;i++){
printf("Enter the marks of %dth student ",i+1);
if(scanf("%d",&a[i])!=1){
fputs("read error\n",stdout);
return 1;
}
sum=sum+a[i];
}
printf("The total sum is %d\n",sum);
return 0;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
What is the problem with the next code ?
It is a code for making a binary search between the elements of an array that we fill randomly using the rand() function. We use here the function bin_sear to sort and return for us a boolean value true if the element we seek is found in our table and flase if the element we dont find doesent figure in our table. The code doesent work. So where is the error here ?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef enum { false, true } bool;
bool bin_sear(int k[] ,int s , int target );
int main()
{
int a[10];
int i,j;
char k;
bool bin;
while(1){
srand(time(NULL));
// we fill our table
for(i=0;i<10;i++){
a[i]=rand()%101+1;
}
printf("Please enter the element you are seeking for: ");
scanf("%d",&j);
if(bin_sear(a[10],10,j)==true){
printf("The element you seek exists in our array");
}
else {
printf("The element you are seeking doesent exist in our array");
}
if (k=='n'){
break;
}
}
return 0;
}
bool bin_sear(int k[],int s, int target){
int i,j,pos,aux,low,high,test;
for (i=0;i<s;i++){
pos=i;
for (j=i;j<s;j++){
if (k[j]<pos){
pos=j;
}
aux=k[pos];
k[pos]=k[i];
k[i]=aux;
}
}
low=0;
high=s;
while (low<high-1){
test=(low+high)/2;
if (target<k[test]){
high=test;
}
else low=test;
}
if (target==k[test]){
return true;
}
else return false;
}
A number of errors in your code.
You need to call bin_sear with the pointer to the array, not element a[10] (which will give you an immediate segmentation fault):
if(bin_sear(a[10],10,j)==true){
should be
if(bin_sear(a,10,j)==true){
Next - take a look at your bubble sort routine. You have the indices wrong, and the braces in the wrong place, and you are comparing a value to an index with your if statement. Modify it to this, and you will get a sorted array:
for (i=0;i<s-1;i++){
pos=i;
for (j=i+1;j<s;j++){
if (k[j]<k[i]){
pos=j;
aux=k[pos];
k[pos]=k[i];
k[i]=aux;
}
}
}
Next, you don't seem to set the value of k anywhere, so you have an infinite loop. Fix all that, and things will work a little bit better. Recommend you put a lot of printf statements in your code if this is not enough (and use a smaller range of random numbers initially to improve your chances of a hit).
Working code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef enum { false, true } bool;
bool bin_sear(int k[] ,int s , int target );
int main()
{
int a[10];
int i,j;
char k;
bool bin;
while(1){
srand(time(NULL));
// we fill our table
for(i=0;i<10;i++){
a[i]=rand()%10+1;
}
printf("Please enter the element you are seeking for: \n");
// scanf("%d", &j); // not scanning input since this is codepad
j = 5;
printf("you entered: %d\n", j);
fflush(stdout);
if(bin_sear(a,10,j)==true){
printf("The element [%d] exists in our array\n", j);
}
else {
printf("The element [%d] doesn't exist in our array\n", j);
printf("The array contains:\n");
for(i = 0; i < 9; i++) printf("%d, ", a[i]);
printf("%d\n", a[9]);
}
printf("Would you like to test for another element?\n");
// k = getchar();
k = 'N'; // simulating user pressing uppercase N
if(tolower(k) == 'n') {
printf("goodbye!\n");
break; // do this only once
}
}
return 0;
}
bool bin_sear(int k[],int s, int target){
int i,j,pos,aux,low,high,test;
printf("starting to sort\n");
fflush(stdout);
for (i=0;i<s-1;i++){
pos=i;
for (j=i+1;j<s;j++){
if (k[j]<k[i]){
printf("swapping %d and %d\n", i, j); fflush(stdout);
pos=j;
aux=k[pos];
k[pos]=k[i];
k[i]=aux;
}
}
}
printf("sort finished\n");
printf("elements now:\n");
for(i = 0; i < s; i++) printf("k[%d] = %d\n", i, k[i]);
fflush(stdout);
low=0;
high=s;
while (low<high-1){
test=(low+high)/2;
if (target<k[test]){
high=test;
}
else low=test;
}
if (target==k[test]){
return true;
}
else return false;
}
if(bin_sear(a[10],10,j)==true){ => if(bin_sea(a, 10, j) == true) from a quick glance. You need to pass the array into the function, a[10] would try to index the eleventh elment of the array, is also the wrong type (being an int instead of an array), and will probably cause an access violation exception, because the array is only ten elements long. Lots of errors for four characters, eh?
It makes no sense to write a function which sorts the array and then makes the binary search. You should separate the two algorithms, otherwise a linear search would be more efficient.