Recursive C program to differentiation a polynomial until it stops - c

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[10],i,n,d[10],power;
float in[10];
clrscr();
printf("Enter the order ofthe polynomial\n");
scanf("%d",&n);
for(i=n;i>=0;i--)
{
printf("Enter the co-efficient of x^%d:",i);
scanf("%d",&a[i]);
}
printf("Given polynomial is\n");
for(i=n;i>=0;i--)
{
if(power<0)
{
break;
}
if(a[i]>0)
{
if(i!=n)
printf(" + ");
}
else if(a[i]<0)
printf(" - ");
else
printf(" ");
printf("%dx^%d",abs(a[i]),i);
}
//To find derivative
for(i=n;i>=0;i--)
d[i]=a[i]*i;
printf("\n Derivative of the given polynomial is\n");
for(i=n;i>=1;i--)
{
if(power<0)
{
break;
}
if(d[i]>0)
printf(" + ");
else if(d[i]<0)
printf(" - ");
else
printf(" ");
printf("%dx^%d",d[i],i-1);
}
getch();
}
the above program only calculate the first derivative, but i need a program which calculates all the derivatives
ex 2x^3+2x^2+3x+1
f1= 6x^2+4x+3
f2=12x+4
f3=12
like this i need to modify the program,, but am not getting how to do that,,please help me//

try this
#include <stdio.h>
#include <stdlib.h>
typedef struct polynomial {
int order;
int *coefficient;
} Polynomial;
void init_poly(Polynomial *p){
int i;
printf("Enter the order of the polynomial\n");
scanf("%d", &p->order);
p->coefficient = malloc((p->order + 1)*sizeof(int));
for(i = p->order; i >= 0; --i){
printf("Enter the co-efficient of x^%d:", i);
scanf("%d", &p->coefficient[i]);
}
}
void drop_poly(Polynomial *p){
free(p->coefficient);
}
void print_poly(Polynomial *p){
int i;
for(i = p->order; i >= 0; --i){
if(p->coefficient[i]){
if(p->order != i){
printf(" %c ", p->coefficient[i] < 0 ? '-' : '+');
printf("%d", abs(p->coefficient[i]));
} else
printf("%d", p->coefficient[i]);
if(i)
printf("x^%d", i);
}
}
printf("\n");
}
void differential_poly(Polynomial *p){
int i;
for(i = 0; i < p->order; ++i){
p->coefficient[i] = p->coefficient[i+1] * (i+1);
}
p->coefficient[p->order--] = 0;
}
void r_print_differential_poly(Polynomial *p){
int i=1;
while(p->order){
differential_poly(p);
printf("f%d = ", i++);
print_poly(p);
}
}
int main(void){
Polynomial poly;
init_poly(&poly);
printf("Given polynomial is\n");
print_poly(&poly);
r_print_differential_poly(&poly);
drop_poly(&poly);
return 0;
}

Related

Why is NULL printed here? - C-90

The code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 500
#define M 100
/*Version 1*/
void replace_word(char *word);
void add_new_word_dictionary(void);
void do_nothing(void);
void return_basic(void);
void check_word(void);
void compute_words(void);
void compute_characters(void);
void compute_ch_sp(void);
void compute_num_dif_words(void);
void create_istogram(void);
void save_file(void);
int get_choice(void);
void return_word(void);
void insert_text(int numwords, char matrix[N][M], int posit);
int main() {
int j;
int choice = 0;
char matrix[N][M];
char word[40] = { "t" };
while (1) {
choice = get_choice();
if (choice == 0) {
insert_text(M, matrix, 1);
}
if (choice == 1) {
add_new_word_dictionary();
}
if (choice == 2) {
do_nothing();
}
if (choice == 3) {
save_file();
}
if (choice == 4) {
compute_words();
}
if (choice == 5) {
break;
}
for (j = 0; j < M; j++) {
printf("%s", matrix[N][M]);
}
}
printf("\n End of Program \n");
return 0;
}
void replace_word(char *word) {
return;
}
void add_new_word_dictionary(void) {
char word[50] = { "s" };
printf("\nPlease enter the word\n");
scanf("\n%s", word);
printf("Your word is %s", word);
return;
}
void do_nothing(void) {
printf("\n do_nothing \n");
return;
}
void return_basic(void) {
printf("\n return basic \n");
return;
}
void check_word(void) {
printf("\n check word \n");
return;
}
void compute_words(void) {
printf("\n compute_words \n");
return;
}
void compute_characters(void) {
printf("\n compute characters \n");
}
void compute_ch_sp(void) {
printf("\n compute_ch_sp \n");
return;
}
void compute_num_dif_words(void) {
printf("\n compute_num_same_words \n");
return;
}
void create_istogram(void) {
printf("\n create istogram \n");
return;
}
void save_file(void) {
printf("\n save_file \n");
return;
}
int get_choice(void) {
int choice = 0;
printf("\n Select a choice from the below \n");
printf("\n Select 0 to add text \n");
printf("\n Select 1 to add new words in the dictionary \n");
printf("\n Select 2 to enter enter correction mode \n");
printf("\n Select 3 to save the text \n");
printf("\n Select 4 to see the statistics about your text \n");
printf("\n Select 5 to exit the program\n");
scanf("\n%d", &choice);
return choice;
}
void insert_text(int numwords, char matrix[N][M], int posit) {
int i;
int j;
char word2[40] = { "" };
while (strcmp(word2, "*T*E*L*O*S*")) {
printf("\n Add the word \n");
scanf("\n%s", word2);
if (posit + 1 > numwords) {
printf("\n Out of Bounds \n ");
}
for (i = numwords - 2; i >= posit; i--) {
strcpy(matrix[i + 1], matrix[i]);
if (!i)
break;
}
strcpy(matrix[posit], word2);
j++;
}
for (i = 0; i < j; i++) {
printf("%s", matrix[i]);
printf("\n j is %d\n", j);
}
return;
}
The problem: I have a function called insert_text. This function adds a string in the 1st position of an array (at least that is what I think it does) and it is called if choice is 0 and executes itself until the string *Ī¤ELOS* is given. When in insert_text I print matrix I get a bunch of *(null)*s... I can count how many words matrix has (by declaring a variable j and incrementing by 1 inside the while loop, but that does not seem to work either. How can I fix this?
The printing code is incorrect: matrix is an array of N arrays of M characters, where you store null terminated C strings. As coded, you pass a single character just beyond the end of the array to printf for %s, which expects a string. The loop should be:
for (j = 0; j < N; j++) {
printf("%s ", matrix[j]);
}
Note that char matrix[N][M]; is uninitialized, so its contents will seem random. Initialize matrix as char matrix[N][M] = { "" };
Also note that in add_new_word_dictionary(), the scanf() conversion should be scanf("%49s", word); to prevent a potential buffer overflow if the user enters a very long word.
Same in insert_text, the code should be scanf("%39s", word2) ans you should test the return value to check for input errors.
Finally, arrays are indexed from 0 in C, so insert_text should be given a position of 0 and the number of words should be N, not M.
Both the test and the insertion loop have problems too.
Here is a modified version:
// call from main as insert_text(N, matrix, 0);
//
void insert_text(int numwords, char matrix[N][M], int posit) {
char word2[40];
int i, j = 0;
for (;;) {
printf("\n Add the word \n");
if (scanf("%39s", word2) != 1) {
break; // end of file or input error
}
if (!strcmp(word2, "TELOS")) {
break; // magic word
}
if (posit >= numwords) {
printf("\n Out of Bounds \n");
break;
}
for (i = numwords - 2; i >= posit; i--) {
strcpy(matrix[i + 1], matrix[i]);
}
strcpy(matrix[posit], word2);
j++;
}
for (i = 0; i < j; i++) {
printf("%s ", matrix[i]);
}
printf("\n j is %d\n", j);
}
Here is a modified version of the whole program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 500
#define M 100
/*Version 1*/
void replace_word(char *word);
void add_new_word_dictionary(void);
void do_nothing(void);
void return_basic(void);
void check_word(void);
void compute_words(void);
void compute_characters(void);
void compute_ch_sp(void);
void compute_num_dif_words(void);
void create_istogram(void);
void save_file(void);
int get_choice(void);
void return_word(void);
void insert_text(int numwords, char matrix[N][M], int posit);
int main() {
int j, done = 0;
char matrix[N][M] = { "" };
while (!done) {
switch (get_choice()) {
case 0:
insert_text(N, matrix, 0);
break;
case 1:
add_new_word_dictionary();
break;
case 2:
do_nothing();
break;
case 3:
save_file();
break;
case 4:
compute_words();
break;
default:
done = 1;
break;
}
for (j = 0; j < N; j++) {
if (matrix[j][0])
printf("%s ", matrix[j]);
}
printf("\n");
}
printf("\n End of Program \n");
return 0;
}
void add_new_word_dictionary(void) {
char word[50];
printf("\nPlease enter the word\n");
if (scanf("%49s", word) == 1)
printf("Your word is %s\n", word);
}
void replace_word(char *word) { printf("\n replace word \n"); }
void do_nothing(void) { printf("\n do_nothing \n"); }
void return_basic(void) { printf("\n return basic \n"); }
void check_word(void) { printf("\n check word \n"); }
void compute_words(void) { printf("\n compute_words \n"); }
void compute_characters(void) { printf("\n compute characters \n"); }
void compute_ch_sp(void) { printf("\n compute_ch_sp \n"); }
void compute_num_dif_words(void) { printf("\n compute_num_same_words \n"); }
void create_istogram(void) { printf("\n create istogram \n"); }
void save_file(void) { printf("\n save_file \n"); }
int get_choice(void) {
int choice = -1;
printf("\nSelect a choice from the below \n");
printf("Select 0 to add text \n");
printf("Select 1 to add new words in the dictionary \n");
printf("Select 2 to enter enter correction mode \n");
printf("Select 3 to save the text \n");
printf("Select 4 to see the statistics about your text \n");
printf("Select 5 to exit the program\n");
scanf("%d", &choice);
return choice;
}
// call from main as insert_text(N, matrix, 0);
//
void insert_text(int numwords, char matrix[N][M], int posit) {
char word2[40];
int i;
for (;;) {
printf("\n Add the word \n");
if (scanf("%39s", word2) != 1) {
break; // end of file or input error
}
if (!strcmp(word2, "TELOS")) {
break; // magic word
}
if (posit >= numwords) {
printf("\n Out of Bounds \n");
break;
}
for (i = numwords - 2; i >= posit; i--) {
strcpy(matrix[i + 1], matrix[i]);
}
strcpy(matrix[posit], word2);
posit++;
}
for (i = 0; i < posit; i++) {
printf("%s ", matrix[i]);
}
printf("\n posit is %d\n", posit);
}

How to check if value exists in struct or not?

I want to ask you guys, here my code cannot check is my value is exist or not in struct, I've input the value, but none of them enter the if else condition, can anyone help me?
#include <stdio.h>
int main(){
int a,i;
struct data {
char nim[10];
};
struct data batas[100];
printf("TEST1 : "); scanf("%[^\n]s", batas[0].nim);
printf("TEST2 : "); scanf(" %[^\n]s", batas[1].nim);
printf("TEST3 : "); scanf(" %[^\n]s", batas[3].nim);
printf("TEST : "); scanf(" %[^\n]s", batas[a].nim);
for(i=0; i<a; i++){
if (batas[a].nim == batas[i].nim) {
printf("Value exist");
} else {
printf("Value doesn't exist");
}
}
return 0;
}
You can not compare array of chars with the equal operator, instead:
if (strcmp(batas[a].nim, batas[i].nim) == 0)
or
if (!strcmp(batas[a].nim, batas[i].nim))
you need to #include <string.h>
Also, note that you are using a uninitialized.
from what you give, it still can't enter the "Value exist" value. Here are my full line of code.
#include <stdio.h>
#include <string.h>
struct data {
char nim[10];
};
struct data batas[100];
int a=0, b, c, d;
int i, j;
char x[20];
void inputdata()
{
printf("\nInput Data\n");
printf("=======================\n");
printf("NIM : "); scanf("%s", batas[a].nim);
for(i=0; i<a; i++){
if (!strcmp(batas[a].nim, batas[i].nim)) {
strcpy(x, "FLAG");
} else {
strcpy(x, "FLAGX");
}
}
printf("%s", x);
a++;
}
void showdata()
{
j=0;
for(i=0; i<a; i++){
j = j + 1;
printf("\nData-%i", j);
printf("\nNIM : %s", batas[i].nim);
}
}
int main() {
int menu;
do {
printf("\nChoose input = "); scanf("%d", &menu);
switch(menu)
{
case 1 : inputdata(); break;
case 2 : showdata(); break;
}
}while (menu != 3);
return 0;
}

Im trying to put return at the end of the code but they keep messing me up with while before return

Like i want to put the "return 0" at the right place, everything in the code is working normal.
complie the code and they keep saying about "while before return", i tried to put outside of the code and inside
#include <stdio.h>
#include <stdlib.h>
#define MAXN 100
int menu(){
printf("Menu:\n");
printf("1- Add a value\n");
printf("2- Search a value\n");
printf("3- Print out the array\n");
printf("4- Print out values in a range\n");
printf("5- Print out the array in ascending order\n");
printf("6- Quit?\n");
printf("Enter your operation: ");
int choice;
scanf("%d", &choice);
return choice;
}
int isFul (int*a, int n){
return n==MAXN;
}
int isEmpty (int*a, int n){
return n==0;
}
void add(int value, int*a, int*pn){
a[*pn] = value;
(*pn)++;
}
int search(int x, int *a, int n){
int i;
for (i=0; i<n; i++) if (a[i]==x) return i;
return -1;
}
void printvalueinrange (int*a, int n){
int i, min, max;
printf("\nEnter min value: ");scanf("%d", &min);
printf("\nEnter max value: ");scanf("%d", &max);
for(i=0; i<sizeof(a); i++)
if(a[i]>=min&&a[i]<=max) printf("%d", a[i]);
}
void printarray(int*a, int n){
int i;
for (i=0;i<n;i++) printf("%d", a[i]);
}
void printAsc(int*a, int n){
int** adds =(int**)calloc(n, sizeof(int*));
int i,j;
for(i=0; i<n; i++) adds[i]= &a[i];
int* t;
for (i=0;i<n-1; i++)
for(j=n-1; j>i; j--)
if (*adds[j]< *adds[j-i]){
t=adds[j];
adds[j]=adds[j-1];
adds[j-1]=t;
}
for (i=0;i<n; i++) printf("%d ", *adds[i]);
free(adds);
}
int main(){
int a[MAXN];
int n=0;
int value;
int choice;
do
{ choice= menu();
switch(choice){
case 1:{
if (isFull(a,n)) printf("\n Sorry! The array is full.\n");
else
{ printf ("Input an added value:");
scanf("%d", &value);
add(value, a, &n);
printf("Added!\n");
}
break;
}
case 2:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{ printf ("Input the searched value:");
scanf("%d", &value);
int pos = search(value, a, n);
if (pos<0) printf("Not found!\n");
else printf("Postion is found: %d\n", pos);
} break;
}
case 3:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{
printarray(a,n);
} break;
}
case 4:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{
printvalueinrange(a,n);
} break;
}
case 5:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{
printAsc(a,n);
} break;
}
default: printf ("Goodbye!");
break;
}
while (choice>0 && choice<7);
getchar();
}
return 0;
}
I just adding some word to fit with the requirement :"D
And if u find out any kind of error or mistake that in my code, just points out for me to improve them xd
In this part of main
}
while (choice>0 && choice<7);
getchar();
}
return 0;
}
the while construction occupies a wrong place.
There should be
}
} while (choice>0 && choice<7);
getchar();
return 0;
}
Also in this statement
if (isFull(a,n)) printf("\n Sorry! The array is full.\n");
^^^^^^
there is a typo. There should be
if (isFul(a,n)) printf("\n Sorry! The array is full.\n");

how to transfer globals to functions and how to calculate the input of arrays?

I am suppoused not to use globals just functions. I don't know where to have int measurements[LENGTH], x, i; to work as it does. I also need a function to calculate de number of measurements(nrOfMeasurements) that user typed in in getMeasurements. for example if they typed in 4 instead f 10. This is needed for ex. to calculate the average.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <limits.h>
#define LENGTH 10
const char *IntroMsg = "\n\t Hello To TEST THE PRGRAM!\n",
*MenuMsg = "\n\t Menu \n\t v (View) \n\t e (Enter) \n\t c (Compute) \n\t r (Reset) \n\t q (Quit) \n\n";
int measurements[LENGTH],//not here
x, i;//not here
void getMeasurements(){
for(x=0; x<LENGTH; x++){
printf("Enter number #%d: ", x+1);
scanf("%d", &measurements[x]);
if(measurements[x]==0){
break;
}
}
return;
}
void getView(){
printf("\t\n");
printf("Your measurements:\n");
if(x>0){
for (i = 0; i < x; i++){
printf("\b %d ", measurements[i]);
}
}
else if(x==0){
printf(" [NO MEASUREMENTS]");
}
printf("\n");
}
void getCompute(){
int min = INT_MAX;
int max = INT_MIN;
int calculus;
float sum;
for(x=0; x<LENGTH; x++){
if (measurements[x]<min) min=measurements[x];
if (measurements[x]>max) max=measurements[x];
sum=sum+measurements[x];
calculus= measurements[x] - (sum/LENGTH);
printf("[%d]", calculus);
}
printf("\nMax number: %d", max);
printf("\nMin number: %d", min);
printf("\nAverage: %.2f", sum/LENGTH);
printf("\n\n");
}
int main(){
while(1){
char choice;
puts (MenuMsg);
scanf(" %c", &choice);
if(choice=='e')
getMeasurements();
else if(choice=='v'){
getView();
}
else if(choice=='c'){
getCompute();
}
else if(choice=='q'){
break;
}
}
return 0;
}
You can do something like this:
#include <stdio.h>
#define MAX_LENGTH 10
const char *IntroMsg = "\n\t Hello To TEST THE PROGRAM!\n";
const char *MenuMsg = "\n\t Menu \n"
"\t v (View)\n"
"\t e (Enter)\n"
"\t c (Compute)\n"
"\t r (Reset)\n"
"\t q (Quit)\n\n";
int getMeasurements(int *measurements, int length) {
int i;
for (i = 0; i < length; i++) {
printf("Enter number #%d: ", i+1);
scanf("%d", &measurements[i]);
if(measurements[i] == 0) {
break;
}
}
return i;
}
void getView(const int *measurements, int length){
int i;
printf("\t\nYour measurements:\n");
if (length == 0) {
printf(" [NO MEASUREMENTS]\n");
return;
}
for (i = 0; i < length; i++) {
printf("\b %d ", measurements[i]);
}
printf("\n");
}
void getCompute(const int *measurements, int length) {
int min, max, calculus, i;
float sum = 0;
if (length == 0) {
printf(" [NO MEASUREMENTS]\n");
return;
}
min = max = measurements[0];
for (i = 0; i < length; i++){
if (measurements[i] < min) min = measurements[i];
if (measurements[i] > max) max = measurements[i];
sum += measurements[i];
calculus = measurements[i] - (sum / length);
printf("[%d]", calculus);
}
printf("\nMax number: %d", max);
printf("\nMin number: %d", min);
printf("\nAverage: %.2f", sum / length);
printf("\n\n");
}
int main() {
int measurements[MAX_LENGTH];
int measurements_count = 0;
int run = 1;
while (run) {
char choice;
puts(MenuMsg);
scanf(" %c", &choice);
switch (choice) {
case 'e':
measurements_count = getMeasurements(measurements, MAX_LENGTH);
break;
case 'v':
getView(measurements, measurements_count);
break;
case 'c':
getCompute(measurements, measurements_count);
break;
case 'q':
run = 0;
break;
default:
printf("Wrong input\n");
break;
}
}
return 0;
}

Copying elements of array to a new one - C

I'm currently doing a school assignment and now I'm really stuck. The problem I have is that When I'm trying to copy the elements of the array dice to the array diceCheck the program goes in to some kind of infinite loop and I don't understand why. Help would be greatly appreciated!
Edit: It's in the bottom in the function printScores.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void printMenu(void);
void throwDice(int dice[], int nrOfDice, int nrOfDieValues);
void readDieValues (int dice[], int nrOfDice);
void printDice(const int dice[], int nrOfDice);
void printScores(const int dice[], int nrOfdice, int nrOfDieValues);
int isThreeOfAKind(const int dieValues[], int nrOfDieValues);
int isSmallStraight(const int dieValues[], int nrOfDieValues);
int main(void)
{
const int nrOfDice = 5;
const int nrOfDieValues = 6;
int dice[4], menuChoice = 0;
printMenu();
printf("\nMake your choice: ");
while(scanf("%d", &menuChoice) != -1)
{
switch (menuChoice)
{
case 0:
printMenu();
break;
case 1:
throwDice(dice, nrOfDice, nrOfDieValues);
printf("Make your choice: ");
break;
case 2:
readDieValues(dice, nrOfDice);
printf("Make your choice: ");
break;
case 3:
printDice(dice, nrOfDice);
printf("Make your choice: ");
break;
case 4:
printScores(dice, nrOfDice, nrOfDieValues);
break;
case -1:
return 0;
break;
default:
printf("Invalid choice!\n");
break;
}
}
return 0;
}
void printMenu()
{
printf("MENU:\n");
printf("0. Display the menu\n");
printf("1. Make a random throw\n");
printf("2. Enter die values for a throw\n");
printf("3. Display the die values for the throw\n");
printf("4. Display the score for the throw\n");
printf("-1. End program\n");
}
void throwDice(int dice[], int nrOfDice, int nrOfDieValues)
{
int choice, i;
printf("Enter seed (1 gives a random seed): ");
scanf("%d", &choice);
if(choice == 1)
{
srand(time(NULL));
for (i = 0; i < nrOfDice; i++)
{
dice[i] = ( rand() % nrOfDieValues) + 1;
}
printf("\n");
}
else
{
srand(choice);
for(i = 0; i < nrOfDice; i++)
{
dice[i] = ( rand() % nrOfDieValues) + 1;
}
printf("\n");
}
}
void readDieValues(int dice[], int nrOfDice)
{
int i;
for(i = 0; i < nrOfDice; i++)
{
printf("Die %d: ", (i+1));
scanf("%d", &dice[i]);
}
}
void printDice(const int dice[], int nrOfDice)
{
int i;
printf("Your dice: ");
for(i = 0; i < nrOfDice; i++)
{
printf("%d ", dice[i]);
}
printf("\n\n");
}
int isThreeOfAKind(const int dieValues[], int nrOfDieValues)
{
}
int isSmallStraight(const int dieValues[], int nrOfDieValues)
{
}
void printScores(const int dice[], int nrOfdice, int nrOfDieValues)
{
int diceCheck[4], i;
for(i = 0; i < nrOfdice; i++)
{
diceCheck[i] = dice[i];
printf("%d ", dice[i]); //these are just for myself to check if it worked
printf("%d ", diceCheck[i]); //these are just for myself to check if it worked
}
}
You have:
const int nrOfDice = 5;
but
int dice[4];
int diceCheck[4];
Your copying idea is correct but you are going one past the end of the array.
To avoid this sort of error, initialize both from the same expression, e.g.:
int dice[4];
const int nrOfDice = sizeof dice / sizeof dice[0];
or
const int nrOfDice = 4;
int dice[nrOfDice];
and inside the PrintScores function, instead of int diceCheck[4];, do:
int diceCheck[nrOfDice];

Resources