ORIGINAL PROBLEM: A number expressed in scientific notation is represented by its mantissa (a fraction) and its exponent (an integer). Define a type sci_not_t that has separate components for these two parts. Define a function scan_sci that takes from the input source a string representing a positive number in scientific notation, and breaks it into components for storage in a sci_not_t structure. The mantissa of an input value (m) should satisfy this condition: 0.1 <= m < 1.0. Also, write functions to compute the sum, difference, product, and quotient of two sci_not_t values. All these functions should have a result type of sci_not_t and should ensure that the result’s mantissa is in the prescribed range. Define a print_sci function as well.
My question pertains to getting and properly storing the mantissa and exponent in the structure. So, the part regarding writing functions for the mathematical operations can be ignored for now. As seen in the code below, I first store the number written in scientific notation in a string as mandated by the problem. My issue is in transferring the numbers from a string to the individual parts of the structure sci_not_t. I will use 0.25000e3 as an example: user enters 0.25000e3, this is stored in a string. I use sscanf to get the double number and integer from the string and store the double part in sci.mantissa and the integer part in sci.exponent. So, 0.25000 is stored in sci.mantissa and 3 would be stored in sci.exponent. I assumed that the compiler would see these as two distinct numbers because of the 'e' between them, however the string "0.25000e3" is actually recognized by the compiler as a number in scientific notation. So, when debugging my code, I found that 250.0 is actually getting stored in sci.mantissa and nothing in sci.exponent. How can I solve this issue? (I commented out parts that can be ignored)
#include <stdio.h>
#include <math.h>
#include <string.h>
typedef struct {
double mantissa;
int exponent;
} sci_not_t;
sci_not_t ScanSci(char *str);
//double Sum2Sci(sci_not_t sci1, sci_not_t sci2);
int main(void)
{
char str1[15];
double sum;
sci_not_t sci1, sci2;
printf("Value 1: ");
sci1 = ScanSci(str1);
printf("Value 2: ");
sci2 = ScanSci(str1);
/*sum = Sum2Sci(sci1, sci2);
printf("Sum: %.5e\n", sum);*/
return 0;
}
sci_not_t ScanSci(char *str)
{
sci_not_t sci;
scanf("%s", str);
sscanf(str, "%lf%d", &sci.mantissa,
&sci.exponent);
while (sci.mantissa < 0.1 || sci.mantissa >=
1.0) {
printf("Error. Mantissa must be in
range [0.1, 1)\n");
scanf("%s", str);
sscanf(str, "%lf%d",
&sci.mantissa, &sci.exponent);
}
return sci;
}
//double Sum2Sci(sci_not_t sci1, sci_not_t sci2)
//{
// return sci1.mantissa * pow(10, sci1.exponent)
+
// sci2.mantissa * pow(10,
sci2.exponent);
//}
#include <stdio.h>
#include <math.h>
#include <string.h>
typedef struct {
double mantissa;
int exponent;
} sci_not_t;
sci_not_t ScanSci(char *str);
//double Sum2Sci(sci_not_t sci1, sci_not_t sci2);
int main(void)
{
char str1[15];
double sum;
sci_not_t sci1, sci2;
printf("Value 1: ");
sci1 = ScanSci(str1);
printf("Value 2: ");
sci2 = ScanSci(str1);
/*sum = Sum2Sci(sci1, sci2);
printf("Sum: %.5e\n", sum);*/
return 0;
}
sci_not_t ScanSci(char *str)
{
int i;
sci_not_t sci;
char mantissa[10],exponent[3]; //declaring strings mantissa and
exponent
scanf("%s", str);
for(i=0;str[i]!='e';i++)
mantissa[i]=str[i]; //storing from staring character to character
before e in mantissa string
i++; //changing the position from e to next character in str
int j=0;
for(i;i<strlen(str);i++)
{
exponent[j]=str[i]; //storing characters after e in exponent
j++;
}
sscanf(mantissa,"%lf",&sci.mantissa); //converting into double
and storing in structure
sscanf(exponent,"%d",&sci.exponent); //converting into integer
and storing in structure
printf("exponent:%d\n",sci.exponent); //printing the values
printf("mantissa:%lf\n",sci.mantissa);
while (sci.mantissa < 0.1 || sci.mantissa >= 1.0) {
//condition check
printf("Error. Mantissa must be in range [0.1, 1)\nenter the value
again\n");
scanf("%s", str);
for(i=0;str[i]!='e';i++)
mantissa[i]=str[i];
i++;
j=0;
for(i;i<strlen(str);i++)
{
exponent[j]=str[i];
j++;
}
sscanf(mantissa,"%lf",&sci.mantissa);
sscanf(exponent,"%d",&sci.exponent);
printf("exponent:%d\n",sci.exponent);
printf("mantissa:%lf\n",sci.mantissa);
}
return sci;
}
//double Sum2Sci(sci_not_t sci1, sci_not_t sci2)
//{
// return sci1.mantissa * pow(10, sci1.exponent) +
// sci2.mantissa * pow(10, sci2.exponent);
//}
Output:
ORIGINAL PROBLEM: A number expressed in scientific notation is represented by its mantissa (a fraction) and...
Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){ char str[1000], ch; int i, frequency = 0; clock_t t; struct timespec ts, ts2; printf("Enter a string: "); gets(str); int len = strlen(str); printf("Enter a character...
I need help with this assignment. My answers are in bold but I am not getting the correct output which is also below. Can you please take a look at it. #include <stdlib.h> #include <stdio.h> #include <float.h> #include <math.h> // PURPOSE: To define a nickname for type 'unsigned int'. typedef unsigned int uInt; //-- Sign related constants --// // PURPOSE: To tell how many bits to shift the sign field from the // least...
I need help with this C code Can you explain line by line? Also can you explain to me the flow of the code, like the flow of how the compiler reads it. I need to present this and I want to make sure I understand every single line of it. Thank you! /* * Converts measurements given in one unit to any other unit of the same * category that is listed in the database file, units.txt. * Handles...
1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...
C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...
Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C - A consecutive group of memory chunks. D - None of the choices. Question 2 How many times is the body of the loop executed? int i=1; while(true) { cout << i; if(++i==5) break; } A - Forever B - 4 C - 5 D - 6 E - 0 Question 3 What is wrong with the following piece of...
/*********************************** * * Filename: poly.c * * ************************************/ #include "poly.h" /* Initialize all coefficients and exponents of the polynomial to zero. */ void init_polynom( int coeff[ ], int exp[ ] ) { /* ADD YOUR CODE HERE */ } /* end init_polynom */ /* Get inputs from user using scanf() and store them in the polynomial. */ void get_polynom( int coeff[ ], int exp[ ] ) { /* ADD YOUR CODE HERE */ } /* end get_polynom */ /* Convert...
In C++ Do not use a compiler like CodeBlocks or online. Trace the code by hand. Do not write "#include" and do not write whole "main()" function. Write only the minimal necessary code to accomplish the required task. Save your answer file with the name: "FinalA.txt" 1) (2 pts) Given this loop int cnt = 1; do { cnt += 3; } while (cnt < 25); cout << cnt; It runs ________ times ...
// Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given a partially completed program that creates a list of patients, like patients' record. // Each record has this information: patient's name, doctor's name, critical level of patient, room number. // The struct 'patientRecord' holds information of one patient. Critical level is enum type. // An array of structs called 'list' is made to hold the list of patients. // To begin, you should trace...
The extended specification of class Polynomial is shown below. You only need to implement the last four methods. The other features (i.e., constructor and operators) are given as part of the solution for tutorial 3. In the .cpp file for the new methods you need to include cmath that contains the definition of pow - raise to power.The goal of this problem set is to extend the solution of tutorial 3. In particular, we wish toadd methods to calculate a...