Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

lgbasallote

  • 2 years ago

how to use double in c programming?

  • This Question is Closed
  1. lgbasallote
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i have a program. the types of my variables are float. But when I replace float with double, the program doesn't run right anymore

  2. lgbasallote
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    let me be specific...

  3. lgbasallote
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    this is my original program: ``` #include <stdio.h> #include <stdlib.h> #include <math.h> #include <ctype.h> void menu(); void polyeval(); float pevalf(float *polynomial); void statalgo(); float tot(float *result); float meanf(float *ave); float stddevf(float *standarddev); void matrix(); int choice, cntr; int degree; float x, sum, peval, constant; float coefficient[10]; int dataNum; float data[10]; float total, mean, square, squaresum, stddev; int row, column, counter, ctr, choicemat, temp = 0; int mat1[3][3], mat2[3][3], summat[3][3], product[3][3], transpose[3][3]; main() { char yn[2]; while (1) { menu(); if (choice == 1) { while (1) { polyeval(); while (1) { printf("\n\n Repeat? <Y/N> "); scanf("%s", yn); yn[0] = toupper(yn[0]); if (yn[0] != 'Y' && yn[0] !='N') { system("cls"); printf("\n\n Invalid choice!"); printf("\n\n\n "); system("pause"); system("cls"); } else break; } if (yn[0] == 'Y') { system("cls"); continue; } else break; } } else if (choice == 2) { while (1) { statalgo(); while (1) { printf("\n\n Repeat? <Y/N> "); scanf("%s", yn); yn[0] = toupper(yn[0]); if (yn[0] != 'Y' && yn[0] !='N') { system("cls"); printf("\n\n Invalid choice!"); printf("\n\n\n "); system("pause"); system("cls"); } else break; } if (yn[0] == 'Y') { system("cls"); continue; } else break; } } else if (choice == 3) { while (1) { matrix(); while (1) { printf("\n\n Repeat? <Y/N> "); scanf("%s", yn); yn[0] = toupper(yn[0]); if (yn[0] != 'Y' && yn[0] !='N') { system("cls"); printf("\n\n Invalid choice!"); printf("\n\n\n "); system("pause"); system("cls"); } else break; } if (yn[0] == 'Y') { system("cls"); continue; } else break; } } else if (choice == 4) return 0; else continue; } } void menu() { system("cls"); printf("\n Main Menu"); printf("\n =========\n\n"); printf("\n 1) Polynomial Evaluation\n"); printf("\n 2) Statistical Algorithms\n"); printf("\n 3) Matrix Operations\n"); printf("\n 4) Exit\n"); printf("\n\n Choice > "); scanf("%d", &choice); system("cls"); } void polyeval() { while (1) { system("cls"); printf("\n POLYNOMIAL EVALUATION"); printf("\n =====================\n"); printf("\n Enter the degree of the polynomial <max 10> "); scanf("%d", &degree); if (degree > 10 || degree < 0) { system("cls"); printf("\n\n Invalid choice!"); printf("\n\n\n "); system("pause"); system("cls"); continue; } if (degree > 0) { printf("\n Enter the value of x > "); scanf("%f", &x); } for (cntr = 0; cntr < degree; cntr ++) { printf("\n Enter the value of coefficient #%d > ", cntr + 1 ); scanf("%f", &coefficient[cntr]); } printf("\n Enter the value of the constant > "); scanf("%f", &constant); if (degree == 0) peval = constant; else pevalf(&peval); printf("\n\n The sum of the polynomial is %f\n", peval); printf("\n\n\n "); system("pause"); break; } } float pevalf(float *polynomial) { sum = coefficient[0]; for(cntr = 1; cntr < degree && cntr < 16; cntr++) sum = sum*x + coefficient[cntr]; *polynomial = sum*x + constant; } void statalgo() { while (1) { system("cls"); printf("\n STATISTICAL ALGORITHMS"); printf("\n ======================\n\n"); printf("\n Enter the number of the data <max 10> "); scanf("%d", &dataNum); if (dataNum <= 0 || dataNum > 10) { system("cls"); printf("\n\n Invalid choice!"); printf("\n\n\n "); system("pause"); system("cls"); continue; } else { for (cntr = 0; cntr < dataNum; cntr++) { printf("\n\n Enter data %d > ", cntr + 1); scanf("%f", &data[cntr]); } system("cls"); printf("\n STATISTICAL ALGORITHMS"); printf("\n ======================\n\n"); printf("\n Select which one you wish to solve for\n"); printf("\n <1> Total\n"); printf("\n <2> Mean\n"); printf("\n <3> Standard Deviation\n"); printf("\n\n\n Choice > "); scanf("%d", &choice); if (choice == 1) { tot(&total); printf("\n\n The total is %f", total); } else if (choice == 2) { meanf(&mean); printf("\n\n The mean is %f", mean); } else if (choice == 3) { stddevf(&stddev); printf("\n\n The standard deviation is %f", stddev); } else { system("cls"); printf("\n\n Invalid choice!"); printf("\n\n\n "); system("pause"); system("cls"); break; } } printf("\n\n\n "); system("pause"); break; } } float tot(float *result) { *result = 0; for (cntr = 0; cntr < dataNum; cntr++) *result = *result + data[cntr]; } float meanf(float *ave) { tot(&total); *ave = total/dataNum; } float stddevf(float *standarddev) { meanf(&mean); squaresum = 0; for (cntr = 0; cntr < dataNum; cntr++) { square = (data[cntr] - mean) * (data[cntr] - mean); squaresum = squaresum + square; } *standarddev = sqrt(squaresum/dataNum); } void matrix() { while (1) { printf("\n Matrix Operations"); printf("\n =================\n"); printf("\n Enter the number of rows of each matrix > "); scanf("%d", &row); printf("\n\n Enter the number of columns of each matrix > "); scanf("%d",&column); if (row < 3 && row > 0 && column < 3 && column > 0) { printf("\n\n\n Enter the elements of the first matrix >\n\n "); for (cntr = 0; cntr < row; cntr++) for (counter = 0; counter < column; counter++) { printf("\n\t Enter element for Row %d Column %d > ", cntr + 1, counter + 1); scanf("%d", &mat1[cntr][counter]); } printf("\n\n\n Matrix 1 is >\n\n"); for (cntr = 0; cntr < row; cntr++) { for (counter = 0; counter < column; counter++) printf(" %d\t", mat1[cntr][counter]); printf("\n\n"); } printf("\n\n Enter the elements of the second matrix>\n "); for (cntr = 0; cntr < row; cntr++) for (counter = 0; counter < column; counter++) { printf("\n\t Enter element for Row %d Column %d > ", cntr + 1, counter + 1); scanf("%d", &mat2[cntr][counter]); } printf("\n\n\n Matrix 2 is >\n\n"); for (cntr = 0; cntr < row; cntr++) { for (counter = 0; counter < column; counter++) printf(" %d\t", mat2[cntr][counter]); printf("\n\n"); } printf("\n\n\n What do you want to do?\n"); printf("\n <1> Sum\n"); printf("\n <2> Product\n"); printf("\n <3> Transpose\n"); printf("\n\n Choice > "); scanf("%d", &choicemat); system("cls"); if (choicemat == 1) { printf("\n Matrix Operations"); printf("\n =================\n"); for (cntr = 0; cntr < row; cntr++) for (counter = 0; counter < column; counter++) summat[cntr][counter] = mat1[cntr][counter] + mat2[cntr][counter]; printf("\n The sum of the matrices is \n\n"); for (cntr = 0; cntr < row; cntr++) { for (counter = 0; counter < column; counter++) printf(" %d\t", summat[cntr][counter]); printf("\n\n"); } } else if (choicemat == 2) { printf("\n Matrix Operations"); printf("\n =================\n"); printf("\n The product of the matrices is \n\n"); for (cntr = 0; cntr < row; cntr++) { for (counter = 0; counter < column; counter++) { for (ctr = 0; ctr < row; ctr++) temp = temp + mat1[cntr][ctr]*mat2[ctr][counter]; product[cntr][counter] = temp; temp = 0; printf(" %d\t", product[cntr][counter]); } printf("\n\n"); } } else if (choicemat == 3) { printf("\n Matrix Operations"); printf("\n =================\n"); printf("\n Select matrix to transpose:\n"); printf("\n <1> Matrix 1\n"); printf("\n <2> Matrix 2\n"); printf("\n\n Choice > "); scanf("%d", &choice); system("cls"); if (choice == 1) { printf("\n Matrix Operations"); printf("\n =================\n"); for (cntr = 0; cntr < row; cntr++) for (counter = 0; counter < column; counter++) transpose[counter][cntr] = mat1[cntr][counter]; printf("\n The transpose of the first matrix is\n\n"); for (cntr = 0; cntr < column; cntr++) { for (counter = 0; counter < row; counter++) printf(" %d\t", transpose[cntr][counter]); printf("\n\n"); } printf("\n\n\n "); system ("pause"); } else if (choice == 2) { printf("\n Matrix Operations"); printf("\n =================\n"); for (cntr = 0; cntr < row; cntr++) for (counter = 0; counter < column; counter++) transpose[counter][cntr] = mat2[cntr][counter]; printf("\n The transpose of the second matrix is\n\n"); for (cntr = 0; cntr < row; cntr++ ) { for (counter = 0; counter < column; counter++ ) printf(" %d\t",transpose[counter][cntr]); printf("\n\n"); } printf("\n\n\n "); system("pause"); } } else { system("cls"); printf("\n\n Invalid choice!"); printf("\n\n\n "); system("pause"); system("cls"); break; } break; } else { system("cls"); printf("\n\n Invalid choice!"); printf("\n\n\n "); system("pause"); system("cls"); break; } } } ```

  4. lgbasallote
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    then this is the program that won't work ``` #include <stdio.h> #include <stdlib.h> #include <math.h> #include <ctype.h> void menu(); void polyeval(); double pevalf(double *polynomial); void statalgo(); double tot(double *result); double meanf(double *ave); double stddevf(double *standarddev); void matrix(); int choice, cntr; int degree; double x, sum, peval, constant; double coefficient[10]; int dataNum; double data[10]; double total, mean, square, squaresum, stddev; int row, column, counter, ctr, choicemat, temp = 0; int mat1[3][3], mat2[3][3], summat[3][3], product[3][3], transpose[3][3]; main() { char yn[2]; while (1) { menu(); if (choice == 1) { while (1) { polyeval(); while (1) { printf("\n\n Repeat? <Y/N> "); scanf("%s", yn); yn[0] = toupper(yn[0]); if (yn[0] != 'Y' && yn[0] !='N') { system("cls"); printf("\n\n Invalid choice!"); printf("\n\n\n "); system("pause"); system("cls"); } else break; } if (yn[0] == 'Y') { system("cls"); continue; } else break; } } else if (choice == 2) { while (1) { statalgo(); while (1) { printf("\n\n Repeat? <Y/N> "); scanf("%s", yn); yn[0] = toupper(yn[0]); if (yn[0] != 'Y' && yn[0] !='N') { system("cls"); printf("\n\n Invalid choice!"); printf("\n\n\n "); system("pause"); system("cls"); } else break; } if (yn[0] == 'Y') { system("cls"); continue; } else break; } } else if (choice == 3) { while (1) { matrix(); while (1) { printf("\n\n Repeat? <Y/N> "); scanf("%s", yn); yn[0] = toupper(yn[0]); if (yn[0] != 'Y' && yn[0] !='N') { system("cls"); printf("\n\n Invalid choice!"); printf("\n\n\n "); system("pause"); system("cls"); } else break; } if (yn[0] == 'Y') { system("cls"); continue; } else break; } } else if (choice == 4) return 0; else continue; } } void menu() { system("cls"); printf("\n Main Menu"); printf("\n =========\n\n"); printf("\n 1) Polynomial Evaluation\n"); printf("\n 2) Statistical Algorithms\n"); printf("\n 3) Matrix Operations\n"); printf("\n 4) Exit\n"); printf("\n\n Choice > "); scanf("%d", &choice); system("cls"); } void polyeval() { while (1) { system("cls"); printf("\n POLYNOMIAL EVALUATION"); printf("\n =====================\n"); printf("\n Enter the degree of the polynomial <max 10> "); scanf("%d", &degree); if (degree > 10 || degree < 0) { system("cls"); printf("\n\n Invalid choice!"); printf("\n\n\n "); system("pause"); system("cls"); continue; } if (degree > 0) { printf("\n Enter the value of x > "); scanf("%f", &x); } for (cntr = 0; cntr < degree; cntr ++) { printf("\n Enter the value of coefficient #%d > ", cntr + 1 ); scanf("%f", &coefficient[cntr]); } printf("\n Enter the value of the constant > "); scanf("%f", &constant); if (degree == 0) peval = constant; else pevalf(&peval); printf("\n\n The sum of the polynomial is %f\n", peval); printf("\n\n\n "); system("pause"); break; } } double pevalf(double *polynomial) { sum = coefficient[0]; for(cntr = 1; cntr < degree && cntr < 16; cntr++) sum = sum*x + coefficient[cntr]; *polynomial = sum*x + constant; } void statalgo() { while (1) { system("cls"); printf("\n STATISTICAL ALGORITHMS"); printf("\n ======================\n\n"); printf("\n Enter the number of the data <max 10> "); scanf("%d", &dataNum); if (dataNum <= 0 || dataNum > 10) { system("cls"); printf("\n\n Invalid choice!"); printf("\n\n\n "); system("pause"); system("cls"); continue; } else { for (cntr = 0; cntr < dataNum; cntr++) { printf("\n\n Enter data %d > ", cntr + 1); scanf("%f", &data[cntr]); } system("cls"); printf("\n STATISTICAL ALGORITHMS"); printf("\n ======================\n\n"); printf("\n Select which one you wish to solve for\n"); printf("\n <1> Total\n"); printf("\n <2> Mean\n"); printf("\n <3> Standard Deviation\n"); printf("\n\n\n Choice > "); scanf("%d", &choice); if (choice == 1) { tot(&total); printf("\n\n The total is %f", total); } else if (choice == 2) { meanf(&mean); printf("\n\n The mean is %f", mean); } else if (choice == 3) { stddevf(&stddev); printf("\n\n The standard deviation is %f", stddev); } else { system("cls"); printf("\n\n Invalid choice!"); printf("\n\n\n "); system("pause"); system("cls"); break; } } printf("\n\n\n "); system("pause"); break; } } double tot(double *result) { *result = 0; for (cntr = 0; cntr < dataNum; cntr++) *result = *result + data[cntr]; } double meanf(double *ave) { tot(&total); *ave = total/dataNum; } double stddevf(double *standarddev) { meanf(&mean); squaresum = 0; for (cntr = 0; cntr < dataNum; cntr++) { square = (data[cntr] - mean) * (data[cntr] - mean); squaresum = squaresum + square; } *standarddev = sqrt(squaresum/dataNum); } void matrix() { while (1) { printf("\n Matrix Operations"); printf("\n =================\n"); printf("\n Enter the number of rows of each matrix > "); scanf("%d", &row); printf("\n\n Enter the number of columns of each matrix > "); scanf("%d",&column); if (row < 3 && row > 0 && column < 3 && column > 0) { printf("\n\n\n Enter the elements of the first matrix >\n\n "); for (cntr = 0; cntr < row; cntr++) for (counter = 0; counter < column; counter++) { printf("\n\t Enter element for Row %d Column %d > ", cntr + 1, counter + 1); scanf("%d", &mat1[cntr][counter]); } printf("\n\n\n Matrix 1 is >\n\n"); for (cntr = 0; cntr < row; cntr++) { for (counter = 0; counter < column; counter++) printf(" %d\t", mat1[cntr][counter]); printf("\n\n"); } printf("\n\n Enter the elements of the second matrix>\n "); for (cntr = 0; cntr < row; cntr++) for (counter = 0; counter < column; counter++) { printf("\n\t Enter element for Row %d Column %d > ", cntr + 1, counter + 1); scanf("%d", &mat2[cntr][counter]); } printf("\n\n\n Matrix 2 is >\n\n"); for (cntr = 0; cntr < row; cntr++) { for (counter = 0; counter < column; counter++) printf(" %d\t", mat2[cntr][counter]); printf("\n\n"); } printf("\n\n\n What do you want to do?\n"); printf("\n <1> Sum\n"); printf("\n <2> Product\n"); printf("\n <3> Transpose\n"); printf("\n\n Choice > "); scanf("%d", &choicemat); system("cls"); if (choicemat == 1) { printf("\n Matrix Operations"); printf("\n =================\n"); for (cntr = 0; cntr < row; cntr++) for (counter = 0; counter < column; counter++) summat[cntr][counter] = mat1[cntr][counter] + mat2[cntr][counter]; printf("\n The sum of the matrices is \n\n"); for (cntr = 0; cntr < row; cntr++) { for (counter = 0; counter < column; counter++) printf(" %d\t", summat[cntr][counter]); printf("\n\n"); } } else if (choicemat == 2) { printf("\n Matrix Operations"); printf("\n =================\n"); printf("\n The product of the matrices is \n\n"); for (cntr = 0; cntr < row; cntr++) { for (counter = 0; counter < column; counter++) { for (ctr = 0; ctr < row; ctr++) temp = temp + mat1[cntr][ctr]*mat2[ctr][counter]; product[cntr][counter] = temp; temp = 0; printf(" %d\t", product[cntr][counter]); } printf("\n\n"); } } else if (choicemat == 3) { printf("\n Matrix Operations"); printf("\n =================\n"); printf("\n Select matrix to transpose:\n"); printf("\n <1> Matrix 1\n"); printf("\n <2> Matrix 2\n"); printf("\n\n Choice > "); scanf("%d", &choice); system("cls"); if (choice == 1) { printf("\n Matrix Operations"); printf("\n =================\n"); for (cntr = 0; cntr < row; cntr++) for (counter = 0; counter < column; counter++) transpose[counter][cntr] = mat1[cntr][counter]; printf("\n The transpose of the first matrix is\n\n"); for (cntr = 0; cntr < column; cntr++) { for (counter = 0; counter < row; counter++) printf(" %d\t", transpose[cntr][counter]); printf("\n\n"); } printf("\n\n\n "); system ("pause"); } else if (choice == 2) { printf("\n Matrix Operations"); printf("\n =================\n"); for (cntr = 0; cntr < row; cntr++) for (counter = 0; counter < column; counter++) transpose[counter][cntr] = mat2[cntr][counter]; printf("\n The transpose of the second matrix is\n\n"); for (cntr = 0; cntr < row; cntr++ ) { for (counter = 0; counter < column; counter++ ) printf(" %d\t",transpose[counter][cntr]); printf("\n\n"); } printf("\n\n\n "); system("pause"); } } else { system("cls"); printf("\n\n Invalid choice!"); printf("\n\n\n "); system("pause"); system("cls"); break; } break; } else { system("cls"); printf("\n\n Invalid choice!"); printf("\n\n\n "); system("pause"); system("cls"); break; } } } ```

  5. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.