int , float problems in c language - c

can some one please explain for me please why i have proplems with the answer when i Enter 2 kined of date type int and float , for example : to fined the minimum element in 2D-array matrix :
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int i,j,m,n;
float min,x[10][10];
puts(" Please Enter the m & n:");//rows & columns
scanf("%d %d",&m,&n);
min = x[0][0];
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf(" Please Enter x[%d][%d]" ,i,j);
scanf("%8.5f",&x[i][j]);
if ( x[i][j] < min )
{
min = x[i][j];
}
}
printf(" minimum in is %8.2f \n", min);
return 0;
}
but if i use int onlye there will be no problems with the answer ?
i try to use both type of data but its got wrong answers .

Related

Ordinal number of the smallest number without array

I need help solving this problem, if anyone had a similar problem it would help me a lot.
The task is:
Write a program that loads n numbers and then prints the smallest as well as its ordinal number.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,d,m,min=1;
printf("Enter as many numbers as you want:");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&m);
if (i == 1)
{
min = m;
}
else if (m < min)
min = m;
}
printf("Min is: %d",min);
}
Thanks in advance !

Random number in c using srand and time not working

Basically I can't figure out what's wrong here. I'm trying randomize the two rand numbers with time, but every time I run it the same numbers show. I really can't tell what's wrong.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i, j, m, n, primeira, tipo, orient;
int matrix[10][20];
time_t tempo = time(NULL);
printf("Enter number of rows : ");
scanf("%d", &m);
printf("Enter number of columns : ");
scanf("%d", &n);
/* first input */
printf("1 ou 0");
scanf("%d", &primeira);
if (primeira == 0) {
matrix [0][0]=0;
matrix [0][1]=0;
matrix [0][2]=0;
matrix [1][0]=0;
matrix [1][1]=0;
matrix [1][2]=0;
matrix [2][0]=0;
matrix [2][1]=3;
matrix [2][2]=0;
}
else
{
srand(tempo);
tipo = rand() % 9;
orient = rand() % 4;

Function gives me a wrong answer

I'm new to C. I've been tasked to run a program that calculates the percentage of students that passed an exam,based on N grade inputs.I don't really understand how functions work in though.This is what I came up with
#include <stdio.h>
#define MAX_N 300
main()
{
int N,grade,i;
float success(N)
{
float sum=0.0;
for (i=0;i<N;i++) {
if (grade>=5) {
sum+=1;
}
float success=sum/N;
return(success);
}
}
printf("How many students? ");
scanf("%d",&N);
printf("Enter grades(0-10) of %d students ",N);
for (i=0;i<N;i++){
scanf("%d",&grade);
}
printf("%f percent of students have passed the exam ",success(N);
return(0);
}
It looks like it should work, however I always get the wrong result.It is stuck on displaying 0.2 or 0.25 for any input I give.Can somebody help?
The problem is that in grade only the last entered data is being stored. Make grade as an array so that all data can be stored.
I guess you are taking multiple value for grade and not taking array for it.
grade should be an array and in loop scanf("%d",&grade[i]); should be implement.
grade should be an array of N integers so that each and every value is stored. You also forgot to multiply success by 100 to get the percentage.
I think I fixed the code:
#include <stdio.h>
#define MAX_N 300
float success(int grade[],int N)
{int i;
float sum=0.0;
for (i=0;i<N;i++) {
if (grade[i]>=5) {
sum+=1;
}
}
float success=sum/N;
return(success*100);
}
int main(){
int N, i;
printf("How many students? ");
scanf("%d",&N);
int grade[N];
printf("Enter grades(0-10) of %d students ",N);
for(i=0;i<N;i++){
scanf("%d", &grade[i]);
}
printf("%f percent of students have passed the exam ", success(grade, N));
return(0);
}
I think you should examine the code I wrote. A little bad code. But it can help.
#include <stdio.h>
int students_success(int *);
int main() {
int n;
printf("How many students?\n");
scanf("%d", &n);
printf("Enter grades(0-10) of %d students\n", n);
int grade;
int pass_std = 0;
for(int i = 0; i < n; ++i) {
scanf("%d", &grade);
pass_std = students_success(&grade);
}
printf("%.2f percent of students have passed exam.\n", (double)pass_std / n);
}
int students_success(int *grade) {
static int pass_std = 0;
if(4 < *grade) {
++pass_std;
}
return pass_std;
}

bad exit when trying to find harmonic mean, geo mean, sum, prod, avg using for loop

Can somebody tell me what is wrong with my code?
#include <stdio.h>
#include <math.h>
int main(void) {
int nSet=0;
int n1,n2;
int sum;
float hm,gm,avg,prod;
printf("Enter two integers: ");
scanf("%d%d",&n1,&n2);
for(;nSet<2;nSet++){
int sum=n1+n2;
float prod=n1*n2;
float hm=nSet/(1/n1+1/n2);
float gm=sqrt(n1+n2);
float avg=(n1+n2)/2;
}
printf("Sum: %d\n",sum);
printf("Product: %4.2f\n",prod);
printf("Average: %4.2f\n",avg);
printf("Geometric mean: %4.2f\n",gm);
printf("Harmonic mean: %4.2f\n",hm);
return 0;
}
originally by initializing in for loop i got 0 as every answer but atleast that printed something. I have to use loops to find the answers and i dont see why for loop would'nt work.
In C if you declare a float and assign a value like 1/2 the result will be 0 because de expression 1/2 is evaluated. If you want to have a result as float put 1.0f/2 or 1/2.0f and will work. In case of having 2 variables and want a float you can do this: (float)n1/n2 or n1/((float)n2) if n1 and n2 are int. Another observation is in the for loop. If you declare your variables again in for they are local in the loop and outside the loop they don't exists.
These code will work:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
int nSet=0;
int n1,n2;
int sum;
float hm,gm,avg,prod;
printf("Enter two integers. \n");
printf("n1 = ");scanf("%d",&n1);
printf("n2 = ");scanf("%d",&n2);
for(;nSet<2;nSet++)
{
sum=n1+n2;
prod=n1*n2;
hm=nSet/(1.0f/n1+1.0f/n2);
gm=sqrt(n1+n2);
avg=(n1+n2)/2.0f;
}
printf("Sum: %d\n",sum);
printf("Product: %4.2f\n",prod);
printf("Average: %4.2f\n",avg);
printf("Geometric mean: %4.2f\n",gm);
printf("Harmonic mean: %4.2f\n",hm);
return 0;
}
You are declaring new var in for scope. When the program exit from the loop all of the var created in for scope will be destroyed.
Your code corrected :
#include <stdio.h>
#include <math.h>
int main(void) {
int nSet=0;
int n1,n2;
int sum;
float hm,gm,avg,prod;
printf("Enter two integers: ");
scanf("%d%d",&n1,&n2);
for(;nSet<2;nSet++){
sum=n1+n2;
prod=n1*n2;
hm=nSet/(1/n1+1/n2);
gm=sqrt(n1+n2);
avg=(n1+n2)/2;
}
printf("Sum: %d\n",sum);
printf("Product: %4.2f\n",prod);
printf("Average: %4.2f\n",avg);
printf("Geometric mean: %4.2f\n",gm);
printf("Harmonic mean: %4.2f\n",hm);
return 0;
}
It's work much better now :)
Enter two integers: 10
1
Sum: 11
Product: 10.00
Average: 5.00
Geometric mean: 3.32
Harmonic mean: 1.00

Odd And Even Counter

I want to change this codes to get 20 Numbers from input and count how many are Odd and how many are Even? please can anyone help?!
#include <conio.h>
#include <stdio.h>
int main()
{
int n;
int odd=0;
int even=0;
printf("\nEnter any number \n");
scanf("%d",&n);
if(n%2!=0)
{
printf("%d is an odd number",n);
odd++;
}
else
{
printf("%d is an even number",n);
even++;
}
printf("\n odd%d / even%d",odd,even);
}
Here is a function that solves your problem:
Tip: You need a loop to take your input 20 times.
void countForJHikaam(){
int n,i;
int odd=0;
int even=0;
for(i=0;i<20;i++){
scanf("%d\n",&n);
if(n%2==0){
even++;
}else{odd++;}
}
printf("Odds: %d, Evens: %d",odd,even);
}
It won't really help you in learning. Now go learn what a function is.
#include <conio.h>
#include <stdio.h>
int main()
{
int n;
int odd=0;
int even=0;
printf("\nEnter any number \n");
while(scanf("%d",&n))
(n%2) ? (++odd) : (++even);
printf("\n odd%d / even%d",odd,even);
}
#include<stdio.h>
main()
{
int odd=0,even=0,no,count=20;
printf("Enter the 20 numbers...\n");
here:
scanf("%d",&no);
(no%2==0)? odd++ : even++ ;`
count--;
if(count>0)
goto here;
printf("No of odd numbers... :%d\n",odd);
printf("No of even numbers... :%d\n",even);
}

Resources