Output error 0 in a basic c program in finding repititions - c

I am trying to write a C program to print the number of repetitions for a given number in an array. what ever value I give as the input , the function returns the value only 0 to me.. what might be the error..the code is attached
#include<Stdio.h>
#include<conio.h>
int occurence(int n, int arr[], int x);
void main()
{
int x,arr[100],n,i;
clrscr();
printf("\nEnter the number of elements: ");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",arr[i]);
}
printf("\nEnter the element to be searched for repetitions: ");
scanf("%d",&x);
printf("%d",occurence(n,arr,x));
getch();
}
int occurence(int n,int arr[100], int x)
{
int i,rep=0;
for(i=0;i<n;i++)
{
if(x == arr[i])
{
rep++;
}
}
return rep;wenter code here
}

Problem is on the line 13 it is supposed to be scanf("%d",&arr[i]);

Related

C program, where I should print array of a SECTION in an array

I built a program in C that is printing average of whole array with pointers and function. I need to make the function print a average of user selected section of an array, for example I put these number in an array: 9,8,2,3,6,4 and I want to make a average from just 2-4-. I am stucked, can you help me please? Thanks.
Code:
#include <stdio.h>
int priemer(int *pole,int n);
int main()
{
int a[100],*pole,n,i;
printf("Zadaj pocet prvkov pola (1-100)");
scanf("%d",&n);
while (n > 100 || n < 1)
{
printf("Chyba! Cislo musi byt v rozmedzi od 1 po 100.\n");
printf("Zadaj pocet prvkov znova: ");
scanf("%d", &n);
}
for(i=0;i<n;i++){
printf("Zadaj %d. prvok - ",i+1);
scanf("%d",&a[i]);
}
pole=a;
priemer(pole,n);
return 0;
}
int priemer(int *pole,int n)
{
int i,c=0;
float p;
for(i=0;i<n;i++)
{
c+=*(pole+i);
}
p=(float)c/n;
printf("Priemer vsetkych prvkov v poli je: %.3f\n",p);
return p;
}

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;
}

Program to accept an array of n elements and count number of palindromes

#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int x,d,a[10],i,sum=0,count=0,n;
cout<<"Enter no of numbers:";
cin>>n;
for(i=0;i<n;++i)
{
cout<<"Enter number"<<i+1<<":";
cin>>a[i];
}
for(i=0;i<n;++i)
{
x=a[i];
while(x!=0)
{
d=x%10;
sum=sum*10+d;
x=x/10;
}
if(sum==a[i])
count++;
}
cout<<"No of palidromes:"<<count;
}
I entered 121,134 and 1331 but the output was always 1. In fact I tried more numbers and still got only 1. Please tell me what's wrong.
Add sum = 0; after your x=a[i];

program for direct diagonalization of a matrix

I need a c program for direct diagonalization of a matrix.
i tried many ways in solving it but didn't get the desired output. so please help me out.
my code
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int n,i,j,k;
float a[10][10],x[10],u,m;
printf("Enter the number of equations :");
scanf("%d",&n);
printf("\nEnter the co-efficients of equations\n");
for(i=1;i<=n;i++)
for(j=1;j<=(n+1);j++)
scanf("%f",&a[i][j]);
printf("\nEntered co-efficient matrix is\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=(n+1);j++)
printf("%.2f\t",a[i][j]);
printf("\n");
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
{
if(i != k)
{
u=a[i][k]/a[k][k];
for(j=1;j<=(n+1);j++)
{
a[i][j]=a[i][j]-(u*a[k][j]);
}
}
}
for(i=1;i<=n;i++)
{
m=a[i][i];
for(j=1;j<=(n+1);j++)
{
a[i][j]=a[i][j]/m;
}
}
printf("\nDiagonalised matrix is\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=(n+1);j++)
printf("%.2f\t",a[i][j]);
printf("\n");
}
printf("\nsolution vector is\n");
for(i=1;i<=n;i++)
{j=(n+1);
printf("x[%d]=%.2f\n",i,a[i][j]);
}
}
output should be like this, if i enter any coefficient ....lower and upper triangular elements should be zero and i should also need to check whether the determinants value remains same or not.

Trying to insert element in an array using pointers, but not successfull

Help me out....There is no compile time error but there is some logical error which i am not able to sort out.
Input is taken from user without any problem but the elements are not getting inserted.
Output is unchanged array that user inputted.
void insert(int*,int);
void main()
{
int a[10];
int i,n,pos,x,j,z;
clrscr();
printf("Enter Size Of an array: ");
scanf("%d",&n);
printf("Enter Elements of an array: ");
for(i=0;i<n;i++)
{
scanf("%d",a+i);
}
insert(a,n);
printf("\nArray after Insertion of elements at 2nd & 5th Position\n");
for(i=0;i<n;i++)
{
printf("\t%d\t",*a+i);
}
getch();
}
void insert(int *b, int n)
{
if(n>=1)
{
printf("Insert Element at 2nd Position: ");
scanf("%d",b+1);
}
if(n>=4)
{
printf("Insert Element at 5th Position: ");
scanf("%d",b+4);
}
}
Your output is wrong, and it's caused by *a+i being interpreted as (*a)+i. This also shows up without any modification when you enter something other than a direct sequence:
Enter Size Of an array: 3
Enter Elements of an array: 1 2 9
gives the output:
1 2 3
which is clearly not right.
The solution, as mentioned in another couple of answers is to wrap your pointer-arithmetic with paranthesis: *(a+i).
"Output is unchanged array that user inputted."
Fix :-
for(i=0;i<n;i++)
{
printf("\t%d\t",*(a+i)); //Notice `()`
}
FYI..this is not insertion, this is just an over-write !
For Insertion you could have something like following :
/*
b= original array
n= size of array (must be large enough)
pos = position of insertion
After call make sure to scan array till n+1
*/
void insert(int *b, int n, int pos)
{
int val,c;
printf("Enter the value to insert\n");
scanf("%d", &val);
for (c = n - 1; c >= pos - 1; c--)
b[c+1] = b[c];
b[pos-1] = val;
}
You should try using parenthesis around the a+i, e.g.
void main()
{
int a[10];
int i,n,pos,x,j,z;
clrscr();
printf("Enter Size Of an array: ");
scanf("%d",&n);
printf("Enter Elements of an array: ");
for(i=0;i<n;i++)
{
scanf("%d",a+i);
}
insert(a,n);
printf("\nArray after Insertion of elements at 2nd & 5th Position\n");
for(i=0;i<n;i++)
{
printf("\t%d\t", /*SEE HERE */ *(a+i));
}
getch();
}
for(i=0;i<n;i++)
{
printf("\t%d\t",*(a+i)); //parenthesis added.
}
The parenthesis around the "(a+i)" are of utmost importance as together with the asterisk it indicates the value within that address. Errors like this are hard to find as they are very small but have a huge impact on the code.
Finally I solved the problem with the help of mistake pointed out by #P0W and #Mats and also applying concept of inserting element at any user defined position. Thanks Guys!!
So Here is the Corrected code. (And it is inserting element and not replacing it).
void insert(int*,int);
void main()
{
int a[10];
int i,n,pos,x,j,z;
clrscr();
printf("Enter Size Of an array: ");
scanf("%d",&n);
printf("Enter Elements of an array: ");
for(i=0;i<n;i++)
{
scanf("%d",a+i);
}
insert(a,n);
printf("\nArray after Insertion of elements at 2nd & 5th Position\n");
for(i=0;i<n;i++)
{
printf("\t%d\t",*(a+i));
}
getch();
}
void insert(int *b, int n)
{
int j;
if(n>=1)
{
printf("Insert Element at 2nd Position: ");
for(j=n-1;j>=1;j--)
{
b[j+1]=b[j];
}
scanf("%d",b+1);
}
if(n>=4)
{
printf("Insert Element at 5th Position: ");
for(j=n-1;j>=4;j--)
{
b[j+1]=b[j];
}
scanf("%d",b+4);
}
}

Resources