C :: Program To Generate A List Of Powers Of 3 - Decimal Places?
Sep 23, 2014
So my assignment requires us to write a C program to generate a list of powers of 3 where each output line lists three numbers: k 3k 3‐k
It should look like this (just pretend all the numbers are lined up in their respective columns)
1 0 1.0
3 1 0.333333343
9 2 0.111111112
27 3 0.037037037
81 4 0.012345679
243 5 0.004115226
729 6 0.001371742
2187 7 0.000457247
6561 8 0.000152416
19683 9 0.000050805
This is what I keep getting:
1 0 1.000000000
3 1 0.333333343
9 2 0.111111112
27 3 0.037037037
81 4 0.012345679
243 5 0.004115226
729 6 0.001371742
2187 7 0.000457247
6561 8 0.000152416
19683 9 0.000050805
My input:
Code:
#include <stdio.h>
#include <stdlib.h>
void powers(void) {
int i=0;
int n=9;
int x=1;
float h=1;
[Code] .....
Notice how I'm getting 1.000000000 for my first h value in my output whereas I should be getting 1.0. What am I doing wrong?
View 6 Replies
ADVERTISEMENT
Jun 7, 2013
So my inventory program runs fine as far as I can tell. Except when entering "Amounts" with more that 1-precision . ie
Amount Entered : 25.99 // Problem
Amount Entered : 25.9 // No Problem
When entering say 25.99, the rest of the file is jibberish. but 25.9, the rest of the file is fine
Code:
#include <stdio.h>
typedef struct toolRecord{
int record_num;
char tool_name[16];
int quantity;
double price;
[Code] .....
View 5 Replies
View Related
Jun 27, 2013
I am trying to find out when using float in a calculation how to set the number of decimal places. For example my code below
Code:
#include stdio.h>
int main()
{
float x=123.0;
[Code]....
This returns an answer 8487.0000 I would like it not to show all the decimal places. However if the sum has decimal places I would like to select the number of decimal places shown.
View 3 Replies
View Related
Sep 2, 2013
I need the user to be able to input the number of decimal places they wish to have displayed in the output. Everything works fine as is, I just don't know how to allow for the user to input the number of decimal places they want the output to have.
Code:
#include<stdio.h>
#include<math.h>
#define PI 3.141592654
int main(void) {
//Local Declarations
int x; //desired number of decimal places
float radius; //radius of circle
float circumference; //circumference of circle
[Code] .....
View 2 Replies
View Related
Dec 1, 2013
I have to create a converter but the results must be in 4 decimal places just like, if i Entered 5000mm the result would be 500.0000cm . what should I do ? I used Float function
View 1 Replies
View Related
Jul 30, 2014
how I can make my output only have 2 decimal places because I want it to be a monetary value. In my code I used this:
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
which worked well at first because on my command prompt screen the numbers had two decimal places and looked liked this:
Premium for Pol1 is $14101.62
Premium for Pol2 is $14221.16
Premium for Pol3 is $582390.50
Premium for Pol4 is $220384.49
However, I also have the program outputting to a textfile called "output.txt" and when I opened this text file the numbers only had one or no decimal places.
Premium for Pol1 is $14101.6
Premium for Pol2 is $14221.2
Premium for Pol3 is $582391
Premium for Pol4 is $220384
How come the code for two decimal places is working for my output to command prompt but not my output to the text file?
Here is my code.
double ratesmn[86] = {
#include "MaleNonSmoker.txt"
- 1
};
[Code].....
View 2 Replies
View Related
Apr 19, 2014
event................. red house.............. blue house........... yellow house
Chess ......................12.................... 5....................................8
TableTennis .................. 4.......................... 11.....................17
Basket Ball .................... 6......................... 5......................14
karathe ..........................5........................... 8.........................10
Write a program to input the house name, and the score (as shown in the table) of the sport event obtained by the corresponding house. Then find the total score . You should use nested selection statements.
i) If the user inputs a wrong house name, display the error message "In valid House Name".
ii) Modify the program to handle many data. After calculate and disp ay the total score of the house, the program should display a prompt "Do you need to enter nother house name?".
If the user inputs "y" or "Y", program should ask for the next house name and the scores.
If the user inputs "n" or "N" after entering all three houses, program should terminate by printing the score of the winner.
If the user inputs "n" or "N" and without entering all three houses, program should display an error message "Error in finding the winner" and terminate without printing the ranks.
Note: format the output into two decimal places.
Sample output:
Enter the house name: red house
Enter the score of Chess: 12
Enter the score of Table Tennis:4
Enter the score of Basket Ball:6
Enter the score of karathe 5
Score is: 27 Points
Do you enter another house name? y
Enter the house name: blue house
Enter the score of Chess:5
Enter the score of Table Tennis:ll
Enter the score of Basket Ball:5
Enter the score of karathe 8
Score is: 29 Points
Do you enter another house name? y
Enter the house name: yellow house
Enter the score of Chess:8
Enter the score of Table Tennis: 17
Enter the score of Basket Ball: 14
Enter the score of karathe 10
Score is: 49 Points
Do you enter another house name? n
The winner scored: 49 Points
View 3 Replies
View Related
Mar 26, 2013
suppose I want to find the value of e ,e^x or sin(x) or any such function in general ,through their infinite series (maclaurin or taylor) how many iteration should I have to make to get the result correct uptu 'n' decimal places?
View 6 Replies
View Related
Aug 13, 2014
I assume floating point numbers in C++ have a default maximum of 5 decimal places. You can use setprecision() but is this limitless?
So how would find say write a program to calculate Pi to N number of places?
View 3 Replies
View Related
Dec 5, 2013
I know how to generate all permutations of a list like: {'h','e','k','p'}
But I am needing to know how to generate all permutations like: {{'a','b'}, {'h','k','d'}, ..... } such that the first character is either an 'a' or a 'b' and the second character is one of the three in the second list, etc. There will be less than 600 000 permutations.
View 3 Replies
View Related
Jun 9, 2013
I'm trying to solve the following problem:
Given a query, find all possible tokens, i.e. query-splits
Example:
query = New York Hotel
tokens = {New, York, Hotel, New York, New Hotel, ...., New Hotel York, Hotel New York, ...}
Given a query with word count n, the total number of tokens is:
n + n*(n-1) + n*(n-1)*(n-2) + ... + n! (any explicit formula available for this sum?)
Now, I have came across various code snippets to generate permutations for a string, but never for a sentence.
View 13 Replies
View Related
Mar 13, 2014
I've a big problem. I'd like to generate of all permutations of objects present on my list and i don't know how to do this. I've found a few examples, but i can't use them in my code...
#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <cstddef>
#include <cstdlib>
#include <iterator>
using namespace std;
class Item {
private:
[Code]...
Input file with objects:
3
2 0 0 4
5 0 1 5
3 0 2 6
and output should be:
2 0 0 4
5 0 1 5
3 0 2 6
2 0 0 4
3 0 2 6
5 0 1 5
3 0 2 6
2 0 0 4
5 0 1 5
etc... 3! so 6 permutations for this example.
I was 'fighting' with this for few days, and I'm so downcast.
View 2 Replies
View Related
Jun 25, 2014
I need to create a list of all members of a struct using the sturct definition as input. The struct is NOT part of the program, but is the input to the program.
The struct that I am using changes over time as features are added. I need the complete, fully qualified field names to then generate a table with all names with their offsets, type and length.
This information then allows me to create readers of the data that will run on different architectures, compilers and operating systems.
The struct currently has 800+ lines and uses typedef and embedded structs.
Perhaps this could be done creating LEX, YACC, Perl, SED, AWK or other language system.
View 6 Replies
View Related
Sep 23, 2014
I am trying to make a program that will convert a list of binary numbers from a file into decimal and print the decimal to the screen. I have no problem doing the conversion, the problem comes up when our teacher wants the input file in a format as such:
3
10110101
11111111
10101010
The first number is supposed to tell the program how many different 8bit strings it is going to have to convert, and then the following lines are those binary numbers.
I am not very experienced with file inputs, and I know how to open files and read lines in.. The problem is, how to say "ok the first line says 3, so now I have to convert the next 3 lines" . I am assuming it is just a simple loop that I am missing....
View 4 Replies
View Related
Oct 24, 2013
I am just starting out programming and have an assignment due in the near future. the problem is I cant seem to figure it out. I started it already but got stuck cant seem to figure it out. Here is the assignment.
Create a program that will generate a list of 200 random numbers (ranging from 1-1000) and determine the medium, mode, and average of the list of numbers. Have the program display the original list ant then display the list in ascending and descending order on the screen.
View 2 Replies
View Related
Sep 22, 2014
How would one write 3^i without using .math? (i being numbers 0-9)
More specifically, this is what I have:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int x, n, answer1, first;
x = 3;
n = 10;
first = 1;
[Code]....
And this is what I want it to produce:
1 0 1.0
3 1 0.333333343
9 2 0.111111112
27 3 0.037037037
81 4 0.012345679
243 5 0.004115226
729 6 0.001371742
2187 7 0.000457247
6561 8 0.000152416
19683 9 0.000050805
I believe I am only having troubles with the first column,
View 10 Replies
View Related
Dec 9, 2014
I want to have a program that will calculate the sum of the fourth powers of 100 real numbers in an array (numbers in this array are stated explicitly in the code), code I have for integers is working perfectly but i want it to work also for non integers.
Code:
#include "stdafx.h"
#include "math.h"
int sum_array(int a[], int numelements) {
long double i, sum = 0;
for (i = 0; i<numelements; i++) {
sum = sum + pow((float)a[i], (int)4);
[code]....
The error messages i have: return' : conversion from 'long double' to 'int', possible loss of data.
View 6 Replies
View Related
Nov 5, 2013
Using a do-while loop, for loop, or while loop. This is what I have so far but i don't know what to do after that.
#include <iostream>
using namespace std;
int main() {
int num;
int min = 2;
int max = 256;
}
Also the results should be printed out with 10 integers per line, right justified in a 5 byte field.
View 1 Replies
View Related
Nov 11, 2014
As I am taking my first steps in C, I study K&R (I guess most of you did the same, right?)
In the introductory chapter about functions (1.7) there is an example showing how to create a function that is then called to calculate powers (b**n). I simplified it to calculate only one given power, 2**5:
Code:
#include <stdio.h>
int power(int m, int n);
main() {
printf("%d", power(2,5));
[Code]....
It will then be called to make the calculation in the above string.
First things first: we already know how to use while(getchar!=EOF) to count characters (K&R chapter 1.5.2) but what if -instead- the input is a specific string? How to "read" it and how to tell my program that the string is finished? And most important: "reading" will be done in the function or in the rest of the body?
View 1 Replies
View Related
Apr 4, 2013
I try to write my data into two different files and i just use something like this
ofstream myfile;
myfile.open("phase 1.txt");
if (myfile.is_open()) {
for(i=0;i<M; i++)
[Code] ....
But it only generate the first file. How should i modify this?
View 3 Replies
View Related
Jul 25, 2013
So I tried debugging this program almost 10 times, each time finding a problem then making code to fix it, I used to get wrong values but then now after I changed the calculation from an infinity while loop to a for loop (because after some changes I realized that now I know the number of entries, meaning the start and the end of the loop) the program now displays no values at all. I checked it thoroughly and until the calculation process its functioning exactly the way it should but then the calculation breaks hell loose . Also the for loop that raises 2 to the power needed works fine too so it must be something with the other processes included in the calculation....
This used to be a function of a multiple value types conversion program, I isolated it for easier analysis as a lone standing program.
Code:
#include<iostream>
using namespace std;
int main() {
int d[10],e[10],anse=1,r,limit;
short int counterd=0,i,j;
float bind=0;
[Code] ....
View 11 Replies
View Related
Jan 30, 2013
I had the idea to write a program that would convert a decimal integer into any base up to 36 (there are no letters after that) so I decided to give it a try.
Code:
#include <stdio.h>
#include <stdlib.h>
char letters(int r); // prototypes function for converting 10's to A's and so on, base 11+ will work
int main() {
int base;
int d; //declares decimal integer
int d_clone; // clones d for a loop
[Code] ......
View 8 Replies
View Related
Oct 31, 2013
so I'm creating a program that generates random sentences with structs..I'm trying to use structs and and create 4 different groupings article, noun, verb, and preposition. Then I'm trying to use "r = rand() % ;" to randomly pick one one word from each group to make a sentence. this is what i have
Code:
typedef enum article {
the = 1, a, one, some, any
} article;
typedef enum noun {
boy = 1, girl, dog, town, car
}
[code]....
View 5 Replies
View Related
Jul 14, 2014
Iam new to c++ and i want to write a program to generate an integer array. I keep getting the error at line 25: invalid types 'int[int]' for array .
int main() {
int rows;
int cols;
cin>>rows>>cols;
int test[rows][cols];
[Code] .....
View 2 Replies
View Related
Oct 15, 2013
Problem: Write a program to generate a calendar for a year. The program should accept the year and the day of the week for January 1 of that year (0 = Sunday, 1 = Monday, etc.). The calendar should be printed in the following form:
January
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
February
1 2 3 4 5
I have wrote code that has three functions, one to print the month, one to print the days in month, and one to find if it is a leap year or not. I have gotten up to the point where I can print the 12 months with a for loop, but how to print out the days in the format above.
This is my code.
#include <iostream>
using namespace std;
void printOneMonth(int month, int& dow, int year);
void daysInMonth(int month, int year, int& dim);
bool isLeapYear(int year);
int main(){
int year=0, dow, count;
[Code]...
View 1 Replies
View Related
Mar 10, 2014
Need to create a program in c++ to generate a symbol table of given assembly language program?
View 1 Replies
View Related