So our teacher gave us a assignment on making a C program to find the solution of linear equation by gauss-jordan method and so I did. It was told that we need to make the program so that it can compute equation having upto 4 variable....The problem i am facing is I want to make it such that the user can use it as many time as he wants but for some random invisible reason when i calculate eqn of 4 variable it gives the answer but the loop ends there however the eqn having 3 variables and below works totally fine.
The CODE is given please HELP ME. Copy-paste to a compiler :)
#include <stdio.h>
#include <math.h>
int main()
{ printf("\t\t\t\t\t\t\t\tPROGRAM TO FIND SOLUTION FOR LINEAR EQUATION UPTO 4 VARIABLE BY GAUSS JORDAN METHOD\n\n\n");
char op = 'y';
while(op =='y')
{
int n;
char var[] = {'x','y','z','w','t'}; //ASSIGNING THE VARIABLE TILL 4
printf("Enter no. of variable : ");
scanf("%d",&n); //ASKING FOR VARIABLE TO KNOW THE NUMBER OF EQUATION AND CALCULATE DIMENSION OF MATRIX
printf("\n\nTHE EQUATION IS IN FORMAT OF ax+by+cz....=k\n\n\n");
float Mat[n][n+1]; //MATRIX DECLARATION
int i,j;
if(n<5)
{
//TAKING INPUT FOR MATRIX
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter the %c coefficient of equation %d : ",var[j],i+1);
scanf("%g",&Mat[i][j]);
}
printf("Enter the constant of equation %d : ",i+1);
scanf("%g",&Mat[i][n]);
printf("\n\n");
}
//PRINTING ALL EQUATIONS
printf("GIVEN EQUATIONS : \n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n+1;j++)
{
if(j<n-1)
{
printf("%g%c + ",Mat[i][j],var[j]);
}
else if(j == n-1)
{
printf("%g%c = ",Mat[i][j],var[j]);
}
else if(j == n)
{
printf("%g",Mat[i][j]);
}
}
printf("\n");
}
//PRINTING THE INITIAL MATRIX
printf("\nTHE INITIAL MATRIX : \n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n+1;j++)
{
if(j==n-1)
{
printf("%g | ",Mat[i][j]);
}
else
{
printf("%g ",Mat[i][j]);
}
}
printf("\n");
}
//SIMPLIFYING THE MATRIX FOR SMOOTHER PROCESS
int change[n+1];
if(Mat[0][0] == 0)
{
int num;
for(int k=0;k<n;k++)
{
if(Mat[k][0]!=0)
{
num = k;
}
}
for(int k=0;k<n+1;k++)
{
change[k] = Mat[0][k];
Mat[0][k] = Mat[num][k];
Mat[num][k] = change[k];
}
}
for(int k=0;k<n+1;k++)
{
Mat[0][k] == Mat[0][k]/Mat[0][0];
}
//PERFORMING GOS JORDAN METHOD
for(int k = 0;k<n-1;k++)
{
for(int i = 1;i<n;i++)
{
float c = Mat[i+k][0+k]/Mat[0+k][0+k];
for(int j = 0;j<n+1;j++)
{
Mat[i+k][j+k] -= c*Mat[0+k][j+k];
}
}
}
//2ND GAUSSIAN CYCLE
for(int k = n;k>0;k--)
{
for(int i = 1;i<n;i++)
{
float c = Mat[k-i-1][k-1]/Mat[k-1][k-1];
for(int j = n;j>-1;j--)
{
Mat[k-i-1][j] -= c*Mat[k-1][j];
}
}
}
//SIMPLIFYING
for(i=0;i<n;i++)
{
for(j=n;j>-1;j--)
{
Mat[i][j] = Mat[i][j]/Mat[i][i];
if(Mat[i][j]== -0)
{
Mat[i][j] = 0;
}
}
}
printf("\n\n\n");
//PRINT END PRODUCT
printf("MATRIX AFTER APPLYING GAUSS JORDAN METHOD : \n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n+1;j++)
{
if(j==n-1)
{
printf("%g | ",Mat[i][j]);
}
else
{
printf("%g ",Mat[i][j]);
}
}
printf("\n");
}
//PRINTING THE SOLUTION OF THE LINEAR EQUATION
printf("\n\nTHE SOLUTION FOR THE GIVEN LINEAR EQUATIONS ARE\n\n");
for(i=0;i<n;i++)
{
printf("%c = %g\n",var[i],Mat[i][n]);
}
printf("Do You want to continue? (y/n) : ");
scanf("\n%c",&op);
}
}
//THE END
}
Why wont the while loop run on a specific 4 variable condition.
Related
This program is supposed to print the first x prime numbers, but I noticed that it was printing some non-prime numbers, such as 27 or 35.
I've been looking at it for hours and nothing seems to pop up. So please, if you know what's wrong, tell me.
#include <stdio.h>
int main(){
int i=0, cont=2, prim=2, quant;
printf("Insert number of prime numbers you wish: ");
scanf("%d", &quant);
printf("The first %d prime numbers are:\n", quant);
while(i<quant){
if(prim%cont!=0 && (cont>1 && cont<prim)){
cont++;
}
else if(prim%cont==0 && (cont>1 && cont<prim)){
prim++;
}
else if(prim%cont==0 && cont==prim){
printf("%d\n", prim);
prim++;
cont=2;
i++;
}
}
return 0;
}
UPDATE
Wow, ok, so after 7 years people still stumble upon this question.
In the last 7 years my coding ability has improved somewhat, and now I see how inefficient this program was. Just in case anyone might be trying to find the solution for this and the stumble upon this, here are three solutions much easier and better, and why they work.
Solution 1:
This first solution is very inefficient too, but it works, and is pretty simple to understand. Basically what you have to do is check if each number up to the upper limit is prime or not. To do so, just check if it is divisible by any number up to its square root.
Why its squared root? Because the square root squared is equal to the number, which means that if the number was not divisible up to its square root, it won't be divisible by any number above it, as for it to be divisible by it, it needs to multiply by a smaller number. For example, the squared root of 36 is 6, and 36 is divisible by 9, as 9*4=36. But since 9 is above 6 (which is the squared root), the number that multiplied by 9 gives us 36 is below, and as such, we have already seen that it is not prime, as we have already checked that 36 is divisible by 4. This being said, if no number below the squared root of a number is a natural dividend of the number, than that number is prime.
#include <stdio.h>
int isPrime(int num) {
for (int i = 2; i*i <= num; i++) {
if (num%i==0) {
return 0;
}
}
return 1;
}
void getPrimes(int num) {
int cont = 0;
for (int i = 2; cont < num; i++) {
if (isPrime(i)==1) {
printf("%d\n", i);
cont++;
}
}
}
int main() {
int quant;
printf("Insert number of prime numbers you wish: ");
scanf("%d", &quant);
printf("The first %d prime numbers are:\n", quant);
getPrimes(quant);
return 0;
}
This is inefficient as we are checking if the number is divisible by numbers that won't influence (more on this in the next solution).
Solution 2:
In this second solution, which is more elegant in my opinion, we give use to the Fundamental Theorem of Arithmetic, which states that any number greater than 1 is either a prime number itself, or can be represented by factorizing into the multiplication of prime numbers. This means that if a number is divisible by a non prime number, it is also divisible by the prime numbers that make it up, and thus we only need to check if the number in question is divisible by smaller prime numbers than itself. For this purpose, we store the prime numbers in an array.
#include <stdio.h>
void getPrimes(int num) {
int primes[num];
int cont = 1;
primes[0] = 2;
int current = primes[cont-1]+1;
while (cont < num) {
int before = current;
for (int i = 0; i < cont; i++) {
if (current%primes[i]==0) {
current++;
break;
}
}
if (before == current) {
primes[cont] = current;
cont++;
current++;
}
}
for (int i = 0; i < cont; i++) {
printf("%d ", primes[i]);
}
printf("\n");
}
int main() {
int quant;
printf("Insert number of prime numbers you wish: ");
scanf("%d", &quant);
printf("The first %d prime numbers are:\n", quant);
getPrimes(quant);
return 0;
}
Solution 3:
This final solution is a mix of the two above. Solution 1 checked numbers that weren't prime, having redundancy, and the solution 2 checked numbers above the square root, which can never be a dividend if the smaller numbers aren't. In this solution, we check if the number is divisible only by prime numbers below the squared root.
#include <stdio.h>
void getPrimes(int num) {
int primes[num];
int cont = 1;
primes[0] = 2;
int current = primes[cont-1]+1;
while (cont < num) {
int before = current;
for (int i = 0; (i < cont && primes[i]*primes[i] <= current); i++) {
if (current%primes[i]==0) {
current++;
break;
}
}
if (before == current) {
primes[cont] = current;
cont++;
current++;
}
}
for (int i = 0; i < cont; i++) {
printf("%d ", primes[i]);
}
printf("\n");
}
int main() {
int quant;
printf("Insert number of prime numbers you wish: ");
scanf("%d", &quant);
printf("The first %d prime numbers are:\n", quant);
getPrimes(quant);
return 0;
}
Comparing the execution times of each algorithm for 100 prime numbers, we obtain the following results
Algorithm 1
Algorithm 2
Algorithm 3
0.048 ms
0.068 ms
0.039 ms
Comparing to the algorithm of the original post which takes 0.599 ms, every one of these new solutions is more efficient (the worst being around 10x better), and actually calculates the real values.
Try this
#include<stdio.h>
int prime(int n)
{
int i, j, len=1, brk=0;
int list[200]={2};
for(i=2; i<=n; i++)
{
for(j=0; j<len; j++)
{
if(i%list[j]==0){
brk=1;
break;
}
else
{
brk=0;
}
}
if(brk==0)
{
list[len]=i;
len++;
}
}
for(i=0; i<len; i++)
printf("%d ",list[i]);
}
main()
{
int i, n;
scanf("%d",&n);
prime(n);
}
#PKDOJ If the series is set within 30. You can go blindly with the logic below. But nothing above that series. Adding i%11 clears the 77 as well. Not efficient
if ((i % 2) && (i % 3) && (i % 5)&&(i%7)&&(i%11))
Code:
int count = 0, quant = 5, i, j;
int flag = 0;
for(prim = 2 ; count <= quant ; prim ++) {
flag = 0;
for(j = 2; j < prim/2; j++) {
if(prim % j == 0) {
flag = 1;
break;
}
}
if(flag == 0) {
printf("%d\n", prim);
count++;
}
}
Update your code as:
while(i<quant){
if(cont<prim) {
if(prim%cont!=0) {
cont++;
} else {
prim++;
cont = 2; // restart cont
}
}
else if(prim%cont==0 && cont==prim){
printf("%d\n", prim);
prim++;
cont=2;
i++;
}
}
Simple way is to find is: if a number is not divisible by 2,3 & 5, its a prime number.
#include <stdio.h>
int main()
{
int num = 35;
int i = 5;
printf("1 2 3 5");
while (i <= num)
{
if ((i % 2) && (i % 3) && (i % 5))
{
printf(" %d",i);
}
i++;
}
printf("\n");
return 0;
}
I need to use array of pointers to multiply the 2 matrices. its working but i m getting the wrong output
.If i give m1=n1=m2=n2 = 2 and both the matrices as 1 2 3 4. I get the o/p matrix as 7 17 32 54 while the right answer is 7 10 15 22. PLEASE HELP ME IM STUCK HERE SINCE 2 DAYS...........................................................................
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m1,n1,m2,n2,i,j,k,sum=0,*a[10],*b[10],*c[10];
printf("Enter m1 and n1 : ");
scanf("%d%d",&m1,&n1);
printf("Enter m2 and n2 : ");
scanf("%d%d",&m2,&n2);
if(n1==m2)
{
for(i=0;i<m1;i++)
{
a[i] = (int *)malloc(n1*sizeof(int));
}
for(i=0;i<m2;i++)
{
b[i] = (int *)malloc(n2*sizeof(int));
}
for(i=0;i<m1;i++)
{
c[i] = (int *)malloc(n2*sizeof(int));
}
printf("Enter the first matrix : ");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
scanf("%d",(*(a+i)+j));
}
}
printf("Enter the second matrix : ");
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
scanf("%d",(*(b+i)+j));
}
}
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
for(k=0;k<n2;k++)
{
sum += a[i][k] * b[k][j];
}
*(*(c+i)+j) = sum;
}
}
printf("The Resultant Matrix : ");
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
printf("%d ",*(*(c+i)+j));
}
printf("\n");
}
}
else
printf("Invalid Matrix\n");
}
You forgot to initialize sum before calculating each elements.
sum = 0; /* add this */
for(k=0;k<n2;k++)
{
sum += a[i][k] * b[k][j];
}
*(*(c+i)+j) = sum;
I'm having issues getting a function to work which should find the determinant of an upper triangular matrix. My code seems to return clearly incorrect values, usually zero and I'm pretty certain that this is caused by me defining the function incorrectly some how. I suspect it is a basic error on my part but after staring at it for sometime I havent managed to figure it out. Here is the function and printing code:
int Determinant(int mat[20][20],int N)
{
int X=0,Det=0;
if (N==2){
Det=mat[0][0]*mat[1][1]-mat[0][1]*mat[1][0];
return(Det);
}
else {
for(X = 0; X < N; X++){
Det *= mat[X][X];
}
}
return (Det);
}
and the print function :
determinant=Determinant(matrix,n);
printf("Determinant = %d",determinant);
I'll include the full code that I've written so far to provide more detail. It's basic application at the moment is to define and n by n matrix (2
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int determinant(int mat[20][20],int N);
int Determinant(int mat[20][20],int N)
{
int X=0,Det=0;
if (N==2){
Det=mat[0][0]*mat[1][1]-mat[0][1]*mat[1][0];
return(Det);
}
else {
for(X = 0; X < N; X++){
Det *= mat[X][X];
}
}
return (Det);
}
int main()
{
int n=0,i=1;
printf("Please enter a number (n) between 2 and 4 to determine the dimensions of an (nxn) matrix \n");
scanf("%d",&n);
while(n<2||n>4){
printf("The value %d does not lie within the required range of 2-4, please re-enter \n",n);
scanf("%d",&n);
i++;
if (i>=3){
printf("\nYou have entered invalid values 3 times. The programme has been terminated");
exit(0);
}
}
printf("\n(%dx%d) matrix selected\n",n,n);
int matrix[n][n];
int f,g=0;
printf("Please enter matrix elements\n");
for(f=0;f<n;f++){
for(g=0;g<n;g++){
printf("Element[%d][%d] = ",f,g);
scanf("%d",&matrix[f][g]);
}
}
int k,j;
printf("\nThe matrix is\n");
for(k=0;k<n;k++){
printf("\n");
for(j=0;j<n;j++){
printf("%d\t",matrix[k][j]);
}
}
int temp=0,c=0,determinant=0;
float factor=0;
k=0;
/* Transform matrix into upper triangular */
for(i = 0; i < n - 1; i++)
{
/* Elementary Row Operation I */
if(matrix[i][i] == 0)
{
for(k = i; k < n; k++)
{
if(matrix[k][i] != 0)
{
for(j = 0; j < n; j++)
{
temp = matrix[i][j];
matrix[i][j] = matrix[k][j];
matrix[k][j] = temp;
}
k = n;
}
}
c++;
}
/* Elementary Row Operation III */
if(matrix[i][i] != 0)
{
for(k = i + 1; k < n; k++)
{
factor = -1.0 * matrix[k][i] / matrix[i][i];
for(j = i; j < n; j++)
{
matrix[k][j] = matrix[k][j] + (factor * matrix[i][j]);
}
}
}
}
printf("\nThe Upper triangular is\n");
for(k=0;k<n;k++){
printf("\n");
for(j=0;j<n;j++){
printf("%d\t",matrix[k][j]);
}
}
determinant=Determinant(matrix,n);
printf("Determinant = %d",determinant);
/*
*/
return 0;
}
The problem is basically the way you pass the matrix as a parameter. To see what I mean, change the definition of the function to read:
int Determinant(int mat[5][5],int N);
and instruct the function body to print the full 5x5 matrix passed:
int Determinant(int mat[5][5],int N)
{
printf("\n");
int a,b;
for(a = 0; a < 5; a++)
{
for(b = 0; b < 5; b++)
{
printf("%d\t", mat[a][b]);
}
printf("\n");
}
int X=0,Det=0;
Det = 1; // Add this too!
for(X = 0; X < N; X++) {
Det *= mat[X][X];
}
return (Det);
}
Now enter n=3 for the matrix dimension and pass the already upper triangular matrix
1 2 3
0 4 5
0 0 6
Observe the printout of the matrix passed in the Determinant() function, it will be something like this:
1 2 3 0 4
5 0 0 6 0
4196432 0 -163754450 0 -1253168992
32764 3 0 0 0
3 0 0 0 3
This means that your array has been "reshaped", and your actual data are stored in consecutive places in memory, unlike the original array.
TLDR: Although I am not very proficient with C, I think that you should define your 2d array as a dynamic one (for example using a double pointer).
PS: Don't forget to initialize Det variable to 1 instead of 0 in the function body, otherwise the product will always equal 0.
I have a method named henry that takes two integer arguments, i and j, and returns the sum of the ith and jth perfect numbers. For example, henry(1, 3) should return 502 because 6 is the 1st perfect number and 496 is the 3rd perfect number and 6 + 496 = 502.
int henry (int i, int j)
{
//how do i start
}
I know how to print perfect number like this:
int main()
{
int i, j, n, sum = 0;
/* Reads upper limit to print perfect numbers upto */
printf("Enter any number to print perfect number up to: ");
scanf("%d", &n);
printf("\nAll Perfect numbers between 1 to %d:\n", n);
/*
* Iterates from 1 to n and print if it is perfect number
*/
for(i=1; i<=n; i++)
{
sum = 0;
/*
* Checks whether the current number i is Perfect number or not
*/
for(j=1; j<i; j++)
{
if(i%j==0)
{
sum += j;
}
}
/* If the current number i is Perfect number */
if(sum == i)
{
printf("%d is Perfect Number\n", i);
}
}
return 0;
}
Please give me hint to solve this problem, thank you.
int henry(int i,int j)
{
int count=0,k=1,s=0;
while(count<=i || count<=j)
{
int sum=0;
for(p=1; p<=k/2; p++)
{
if(k%p==0)
{
sum += p;
}
}
if(sum==k)
count++;
if(count==i||count==j)
s+=k;
k++;
}
if(i!=j)
return s;
else
return 2*s;
}
I'm trying to code a program to find the frequency of a number without asking for the user input.To be clear,I'm looking for an idea where the user doesnot enter the value for which he needs to calculate the frequency,the compiler should default tell the frequency of each number in the list instead of user giving a number .Below mentioned is my code.
#include <stdio.h>
#include<conio.h>
float frequency (int theArray [ ], int number, int x)
{
int count = 0;
float mount=0.0;
float tot = (float)number;
int u;
int k;
float q,h;
// printf("%d",number);
for (u = 0; u < number; u++)
{
if ( theArray[u]==x)
count++;
}
printf ("\nThe frequency of %d in your array is %d ",x,count);
for(k = 0; k< number; k++)
{
if(theArray[k]==x)
//count(theArray[k])
{ mount++;
}
}
if(mount>1)
{
q = mount/tot;
return q;
}
else
{
h = mount/tot;
return h;
}
}
void main()
{
FILE*file = fopen("num.txt","r");
int integers[100];
int theArray[100];
int i=0;
float e;
int num;
int x,k;
while(fscanf(file,"%d",&num)>0)
{
integers[i]=num;
printf("\n%d",integers[i]);
i++;
}
printf ("\n OK, Thanks! Now What Number Do You Want To Search For Frequency In Your Array? ");
scanf(" %d", &x);/*Stores Number To Search For Frequency*/
e = frequency(integers,i,x);
printf("\n probability of %d is %f",x,e);
getch();
fclose(file);
}
so my output as of now is:
1) The list of numbers
2)The frequency of the number occurence
3)Probability of my number
instead i'm looking for something that gives the probability and frequency of my number without any
user input. i.e I'm looking for something like
Output:
Frequency of this number is ---- and it's probability is---------
Any help would be highly appreciated.
this loop will do the trick for u
for(i = 0; i < number; i++)
{
if(array[i] == -999) // checking if the number has been counted before or not
continue;
temp = array[i]; // take each number from the array one by one
for(j = 0; j < number; j++) // iterate over the array to check the number of occurences of temp
{
if(array[j] == temp)
{
array[j] = -999; // putting -999 at places where the number has already been counted
count++;
}
}
printf("frequency of %d is %d, and probability is %f", temp, count, count/number);
count = 0;
}