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;
}
Related
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 8 months ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
//odd number generator with int k as the max number
void genap_generator(){
int k;
printf("Masukkan batas bilangan genap = ");
scanf("%i", &k);
printf("Bilangan genap dari 0 sampai %i adalah :\n");
for (int i=0; i<=k;i+=2){
printf("%i\n", i);
}
}
int main(){
genap_generator();
system("pause");
}
I made a program to generate odd numbers with int k as the max number but when i print the integer it doesnt print kprint error
in the line where you want to print k: printf("Bilangan genap dari 0 sampai %i adalah :\n");, you didnt pass k.
The line should be: printf("Bilangan genap dari 0 sampai %i adalah :\n", k);.
What the function does print right now is what is placed on the stack where k should have been.
Also, you are printing all the even numbers, if you want to print the odd numbers start from i=1.
Welcome to our community! Could you use English language in your code next time?
The problem was with printing in printf, you did not declare which variable you wanted to print. Here is the working code:
#include <stdio.h>
#include <stdlib.h>
//odd number generator with int k as the max number
void genap_generator()
{
int k;
printf("Enter an even number limit = ");
scanf("%i", &k);
// Had to declare, which variable to print
printf("The even numbers from 0 to %i are:\n", k);
for (int i = 0; i <= k; i += 2)
{
printf("%i\n", i);
}
}
int main()
{
genap_generator();
system("pause");
}
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 10 months ago.
Improve this question
I am beginner in C. I have been writing a program code to find largest element in Array.
Passed Value is not able to store in array of type: float.
Instead default Value:0 is being stored.
What to do?
#include <stdio.h>
// Largest Element
int main() {
int n,i;
float arr[100];
printf("Enter Element Count in range(1,100):-");
scanf("%d",&n);
while(n>100 || n<1){
printf("Enter Element Count in range(1,100) again:-");
scanf("%d",&n);
}
for(i=0; i<n;i++){
printf("Enter Element:-");
scanf("%f",&arr[i]);
}
for(int k=0;k<n;k++){
printf("Element-%d:-%d\n",k+1,arr[k]);
}
for(int j=0;j<n;j++){
for(int k=j+1;k<n;k++){
if (arr[j] < arr[k]){
break;
}
else{
printf("Largest Element:-%d\n",arr[j]);
break;
}
}
continue;
}
return 0;
}
OUTPUT SCREEN
Explanation
In the two cases below you are using %d to print a float number. That's why you are getting 0 printed and not the actual number.
Use %f like you did for reading the value on scanf earlier instead of %d for the float values.
printf("Element-%d:-%d\n",k+1,arr[k]);
printf("Largest Element:-%d\n",arr[j]);
To output values of the type float you have to use the conversion specifier f. The conversion specifier d is designed to output integer values.
So these calls of printf
printf("Element-%d:-%d\n",k+1,arr[k]);
and
printf("Largest Element:-%d\n",arr[j]);
are incorrect. You have to write
printf("Element-%d:-%f\n",k+1,arr[k]);
and
printf("Largest Element:-%f\n",arr[j]);
Also if you are going to find the largest element in the array then these for loops do not make a sense.
for(int j=0;j<n;j++){
for(int k=j+1;k<n;k++){
if (arr[j] < arr[k]){
break;
}
else{
printf("Largest Element:-%d\n",arr[j]);
break;
}
}
continue;
}
At least for example the continue statement is redundant. And the inner for loop is also redundant because it is interrupted at once due to break statements in the if-else statement.
The largest element is searched the following way using only one for loop.
int largest = 0;
for ( int i = 1; i < n; i++ )
{
if ( arr[largest] < arr[i] ) largest = i;
}
printf( "Largest Element:-%f at position %d\n",arr[largest], largest );
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 1 year ago.
Improve this question
Hi I was study for computer and we got this assignment to do break statement in c
this is my code
#include<stdio.h>
int main()
{
float num, sum=0;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d", &n);
for(i=1; i<=n; ++i);
{
printf("Enter number %d: ", i);
scanf("%f", &num);
if(num<0.0)
break; //for loop breaks if num < 0.0
sum = sum + sum;
}
printf("Total addition: %.2f ", sum);
return 0;
}
I already copy like my lecture instruct but I got the error break statement is not within loop
or switch. I don't know were wrong so I hope you can tell the error and some explain for me thank you
You have a stray ; after your for loop:
// here ------v
for(i=1; i<=n; ++i);
{
This means you actually have a for loop with an empty body followed by a standalone block statement, so the break is not inside of a loop.
Get rid of the extra ; so that the block statement is the body of the loop.
for(i=1; i<=n; ++i)
{
On the line with for(i=1; i<=n; ++i); you have an added semicolon that prevents the contents of the for loop from being executed. The corrected code would be as follows:
#include<stdio.h>
int main()
{
float num, sum=0;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d", &n);
for(i=1; i<=n; ++i)
{
printf("Enter number %d: ", i);
scanf("%f", &num);
if(num<0.0)
break; //for loop breaks if num < 0.0
sum = sum + sum;
}
printf("Total addition: %.2f ", sum);
return 0;
}
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 3 years ago.
Improve this question
This is my attempt at recursion. It compiles and runs, but doesn't display the factorial of the number that I input. I'm attempting this with Geany on Ubuntu.
#include <stdio.h>
int fact(int n);
int main() {
int n;
printf("Give me a number");
scanf("%6d", &n);
fact(n);
}
int fact(int n) {
if (n <= 1)
return 1;
else
return n * fact(n - 1);
}
You are missing the print statement.
You could save the result in a variable and then print it.
printf("%d",fact(n));
Your code is fine, but you forget to to print the return value of function fact() change this portion of your code
scanf("%6d", &n);
fact(n);
to this:
scanf("%6d", &n);
printf("%d", fact(n));
Your work will done. after replacing your main() function will look like:
int main()
{
int n;
printf("Give me a number");
scanf("%6d", &n);
printf("%d", fact(n));
}
Note: The factorial of 17 or higher is not adjust in integer limit.
doesn't display the factorial of the number that I input.
in
int main()
{
int n;
printf("Give me a number");
scanf("%6d", &n);
fact(n);
}
you do not print the result of factorial, then it is not print
why are you using "%6d" rather than "%d" in the scanf ? you do not print so you do not need that
I also encourage you
to add a separator after Give me a number else the input number will seem attached to it (I used a ':' below)
to test the result of scanf
So, for instance :
int main()
{
int n;
printf("Give me a number:");
if (scanf("%d", &n) == 1)
printf("fact(%d)=%d\n", n, fact(n));
}
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