|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
Problem D: Repeating the Program Run In this part, you must embed all of Part C in a FOR-Loop that runs N times. At the start, prompt for an integer value of N, read it in and run the loop. So, if N is typed in at the prompt as 2, your solution to Part C will first run once, prompting for the two state names, etc., and will give its result; then, it will prompt a second time and read in a set of two states and their votes and give a result.
Basically that's what I have to do....if I use: for(i=0; i < timestorun; i++) and have int=i; and int timestorun=2 it will run PERFECT, but if I prompt the user printf("How many times do you want to run this program?\n"); scanf("%d", timestorun);
it won't work :/
edit: Wow, nevermind...I forgot the ×torun
DUH. I just needed to retype it to see what I was missing. I haven't learned how to "debug" yet. Any recommendations ?
edit2: Sorry, it's really hard to explain it sometimes. This is all brand new to me. Only my second week doing this.
|
Hello I'm new to programming and I have to figure out an algorithm, the Formula of Brouncker for 4/pi William Brouncker
Sorry for the french because the english one doesn't show the right formula
I know how to do it on paper but it seems getting it into Python is a bit different so I could use some help :D The user of the program as to input a precision value in real between 0 and 1 exclusively.I have to approximate the differences between Brouncker's formula, and pi, and the preset value of the desired accuracy between the two. And print out how many times it went into the loop.
Python 3.2.2 The last line is to know what value the formula have so far in the loop
My code not working
|
Germany1338 Posts
On January 25 2012 03:03 Lorac wrote: I need to find the number of the formula within that precision of the real PI. ..what?
First: Speak english. "the precision of real pi" makes absolutely no sense. the "real pi" is an irrational number, it doesn't have a precision. Also you might want to link to the english wikipedia instead of the french. My french is a little .. no longer existent you know? 
Second: Upload python code to pastebin.com for instance - anywhere really where the indentation is not stripped. Otherwise nobody can figure out your loops. ^^
R1ch should implement a [code] tag..
|
On January 25 2012 00:35 WerderBremen wrote: I'm missing low level language - assembler. I got decent knowledge about programming on x8086 and a few other 16 bit models. Anybody else here who is programming with low level language (even others than assembler)? Would be interesting to know.
We did a little bit assembler in school and in the academy. Its not really existant at work where we mainly use C/C++, C#, Java or Perl depending on whats needed to be done. Do you use Assembler at work or is it just a private infantuation?
|
On January 25 2012 03:03 Lorac wrote:Hello I'm new to programming and I have to figure out an algorithm, the Formula of Brouncker for 4/pi William BrounckerSorry for the french because the english one doesn't show the right formula I know how to do it on paper but it seems getting it into Python is a bit different so I could use some help :D The user of the program as to input a precision value in real between 0 and 1 exclusively.I have to approximate the differences between Brouncker's formula, and pi, and the preset value of the desired accuracy between the two. And print out how many times it went into the loop. Python 3.2.2 The last line is to know what value the formula have so far in the loop My code not working
in your mid-part you do:
while (abs((((1+total)) - 4/pi)) >= x): n = n / (((n+2)**2)) + 2 total = total + 2 n = n + 2 count = count + 1
Right there something is fundamentally wrong. Why do you randomly increase n and total by 2? That doesn't happen in the brouncker equation.
The problem with the brouncker equation is that the next step doesnt' add up to your current total just like that if you don't want it to be easymode. You would have to use recursion for the real solution.
Otherwise just use the rush-strategy:
while (abs((((total)) - 4/pi)) >= x): if (n%2){ total = total + 1/(1+2*n); }else{ total = total - 1/(1+2*n); } n = n+1
|
On January 25 2012 05:23 silentsaint wrote:Show nested quote +On January 25 2012 03:03 Lorac wrote:Hello I'm new to programming and I have to figure out an algorithm, the Formula of Brouncker for 4/pi William BrounckerSorry for the french because the english one doesn't show the right formula I know how to do it on paper but it seems getting it into Python is a bit different so I could use some help :D The user of the program as to input a precision value in real between 0 and 1 exclusively.I have to approximate the differences between Brouncker's formula, and pi, and the preset value of the desired accuracy between the two. And print out how many times it went into the loop. Python 3.2.2 The last line is to know what value the formula have so far in the loop My code not working in your mid-part you do: Show nested quote +while (abs((((1+total)) - 4/pi)) >= x): n = n / (((n+2)**2)) + 2 total = total + 2 n = n + 2 count = count + 1
Right there something is fundamentally wrong. Why do you randomly increase n and total by 2? That doesn't happen in the brouncker equation. The problem with the brouncker equation is that the next step doesnt' add up to your current total just like that if you don't want it to be easymode. You would have to use recursion for the real solution. Otherwise just use the rush-strategy: while (abs((((total)) - 4/pi)) >= x): if (n%2){ total = total + 1/(1+2*n); }else{ total = total - 1/(1+2*n); } n = n+1
the brouncker works like that : 1+1^2/(2+3^2/(2+5^2/(2+7^2...........))) so it adds 2 everytime
|
I did change a big my calculation of the formula and i came up with
v = ((1 + 2*n)**2) / (2 + v)
v being the last value
but now i want to tell him do this till n>=1 and i have to v = 1 / (2 + v) pib = (1 + v) pib is pi brounker
and i have to compare pib and pi and check if the precision i asked in the first input is good between both like 0.001 difference between both else n = n + 2
|
I have a question about a program I recently wrote in C.
In it, I had a function that I wanted to do some operations on two specific indices in two different arrays. My first inclination was that I should write a function that looks like this:
int doSomethingWithArrays(int* array[1], int* otherArray[2]){ edit these values; }
Obviously, that didn't work. But hopefully that communicates what I was trying to accomplish. In the end I ended up passing both the arrays and the specific indices as arguments. But that resulted in 4 arguments for my function. Is there a way to do what I was trying to do? (If you're confused, all I was trying to do is pass a pointer to an element in an array as an argument. )
Is this even possible? Or do I have a terribly flawed understand of what's going on?
|
On January 26 2012 04:55 scarymeerkat wrote: I have a question about a program I recently wrote in C.
In it, I had a function that I wanted to do some operations on two specific indices in two different arrays. My first inclination was that I should write a function that looks like this:
int doSomethingWithArrays(int* array[1], int* otherArray[2]){ edit these values; }
Obviously, that didn't work. But hopefully that communicates what I was trying to accomplish. In the end I ended up passing both the arrays and the specific indices as arguments. But that resulted in 4 arguments for my function. Is there a way to do what I was trying to do? (If you're confused, all I was trying to do is pass a pointer to an element in an array as an argument. )
Is this even possible? Or do I have a terribly flawed understand of what's going on? It depends. Is the function part of a class? For instance, if you create an object with the two arrays stored by the constructor, you can just pass the indices in the function.
If the function is not part of a class, then no, I think you're stuck with 4 arguments, unless you can hardcode the indices in the function, rather than passing them as parameters.
|
.... that wouldn't be possible in C, would it? C isn't object-oriented. I didn't mean C++.
So I guess I'm stuck with 4 arguments and a lot of typing. So you for sure can't create a pointer to a member of an array like you would with a normal int?
|
On January 26 2012 08:39 scarymeerkat wrote: .... that wouldn't be possible in C, would it? C isn't object-oriented. I didn't mean C++.
So I guess I'm stuck with 4 arguments and a lot of typing. So you for sure can't create a pointer to a member of an array like you would with a normal int? you definitely can. I'm not completely sure, but try for example *(array+1) instead of *array[1]
EDIT: wait. scratch that. If you're creating the function, you should just use an int pointer (and call it whatever, just don't even mention it is an array value).
EDIT2: yeah see the guy who posted after me
|
If you have an array of some type you can pass variables inside that array to a function like this:
+ Show Spoiler + void someFunction(int *var) { //do whatever }
//somewhere else int array[someconstant] = {...whatever...};
someFunction(&array[your_index]);
|
When I try *array[1] I get a compile time error: 42: error: invalid type argument of ‘unary *’ (have ‘int’)
In fact, not 100% sure because I forget how I might have changed the code while super tired last night, but I think what you were saying is what I had. I keep getting seg faults if I tinker with things til it compiles.
EDIT: thanks heishe, that works perfectly. definitely good to know. Ignore what I just said, everything works fine now.
|
Are you guys aware of a way to create dynamic structs, or create an unfixed number of strings during a program?
|
On January 26 2012 08:57 Ninja_Bread wrote: Are you guys aware of a way to create dynamic structs, or create an unfixed number of strings during a program?
You mean an array whose size isn't fixed at runtime? In C/C++ it's malloc/new, in C#/java/python there are list collections you can use.
|
Question: Write a complete C program that uses a FOR loop to read in 41 integers. For each of the 41 integers, first multiply the integer by itself; if the result obtained (ie the squared value) is greater than 500, the original integer (before it was squared) should be added into a sum that was initalized to zero. After the loop, multiply the sum by itself and then print out the answer as an integer. This is a review question for our test. I'm kind of stuck to be honest. It's not graded or anything, but there will be a similar question on the test tomorrow and I'd like to understand how/why this isn't working for me. I've put my notes in the // sections of the code
#include <stdlib.h> #include <stdio.h>
int main(void) {
int total; int sum=0; int number;
//I'm confused big time...I enter a #, number... //Then i do an IF state ment to say if that number*number is greater than 500 //Then you should add the sum (initalized at zero) to the original number, which is number //After 41 attempts, it takes the numbers in the sum and multiplies itself. //How far am I off? //Right now, it just continuously will ask for numbers over and over, but if i enter a number //over 41, it will say "your answer is 0" for(number; number <=41; number++){ printf("What is your number\n"); scanf("%d", &number);}
if(number*number>500){ total=sum+number;}
total=sum*sum;
printf("The total is %d.\n",total);
system("pause"); return 0;}
|
On January 26 2012 11:45 ranshaked wrote: Question: Write a complete C program that uses a FOR loop to read in 41 integers. For each of the 41 integers, first multiply the integer by itself; if the result obtained (ie the squared value) is greater than 500, the original integer (before it was squared) should be added into a sum that was initalized to zero. After the loop, multiply the sum by itself and then print out the answer as an integer. This is a review question for our test. I'm kind of stuck to be honest. It's not graded or anything, but there will be a similar question on the test tomorrow and I'd like to understand how/why this isn't working for me. I've put my notes in the // sections of the code
#include <stdlib.h> #include <stdio.h>
int main(void) {
int total; int sum=0; int number;
//I'm confused big time...I enter a #, number... //Then i do an IF state ment to say if that number*number is greater than 500 //Then you should add the sum (initalized at zero) to the original number, which is number //After 41 attempts, it takes the numbers in the sum and multiplies itself. //How far am I off? //Right now, it just continuously will ask for numbers over and over, but if i enter a number //over 41, it will say "your answer is 0" for(number; number <=41; number++){ printf("What is your number\n"); scanf("%d", &number);}
if(number*number>500){ total=sum+number;}
total=sum*sum;
printf("The total is %d.\n",total);
system("pause"); return 0;}
This seems like a bit too close to a homework question, so I'm going to just remark on a few things and let you figure out the rest.
First, you're using your loop variable as the user input variable. That is not correct.
Second, look at where your loop is ending, compared to the rest of the program logic.
Once you fix those two problems, review your usage of total and sum.
|
On January 26 2012 12:23 destian wrote:Show nested quote +On January 26 2012 11:45 ranshaked wrote: Question: Write a complete C program that uses a FOR loop to read in 41 integers. For each of the 41 integers, first multiply the integer by itself; if the result obtained (ie the squared value) is greater than 500, the original integer (before it was squared) should be added into a sum that was initalized to zero. After the loop, multiply the sum by itself and then print out the answer as an integer. This is a review question for our test. I'm kind of stuck to be honest. It's not graded or anything, but there will be a similar question on the test tomorrow and I'd like to understand how/why this isn't working for me. I've put my notes in the // sections of the code
#include <stdlib.h> #include <stdio.h>
int main(void) {
int total; int sum=0; int number;
//I'm confused big time...I enter a #, number... //Then i do an IF state ment to say if that number*number is greater than 500 //Then you should add the sum (initalized at zero) to the original number, which is number //After 41 attempts, it takes the numbers in the sum and multiplies itself. //How far am I off? //Right now, it just continuously will ask for numbers over and over, but if i enter a number //over 41, it will say "your answer is 0" for(number; number <=41; number++){ printf("What is your number\n"); scanf("%d", &number);}
if(number*number>500){ total=sum+number;}
total=sum*sum;
printf("The total is %d.\n",total);
system("pause"); return 0;}
This seems like a bit too close to a homework question, so I'm going to just remark on a few things and let you figure out the rest. First, you're using your loop variable as the user input variable. That is not correct. Second, look at where your loop is ending, compared to the rest of the program logic. Once you fix those two problems, review your usage of total and sum. It's not homework :/ just review for the exam...basically he gave us some problems to figure out for the test. I changed the variables, I didn't realize that the loop was the same. I'm using a different variable. I also changed the loop ending to right before the line "total=sum*sum" because that line is after the loop, but everytime I put in numbers I always get an answer of "0"
|
Kentor
United States5784 Posts
that's where you end your braces? my head hurts...
and you need to initialize your loop counter variable. plus you are never changing the variable sum. look at your program. at what point do you assign the variable sum anything new? certainly not this line
total=sum+number;
|
On January 25 2012 03:49 MisterD wrote:Show nested quote +On January 25 2012 03:03 Lorac wrote: I need to find the number of the formula within that precision of the real PI. ..what? First: Speak english. "the precision of real pi" makes absolutely no sense. the "real pi" is an irrational number, it doesn't have a precision. Also you might want to link to the english wikipedia instead of the french. My french is a little .. no longer existent you know?  Second: Upload python code to pastebin.com for instance - anywhere really where the indentation is not stripped. Otherwise nobody can figure out your loops. ^^ R1ch should implement a [c0de] tag..
There is a [c0de] tag though....
public class CodeTagExample { public static void main(String[] args) { System.out.println("it werks!); } }
seems to break if you have more than one [c0de] tag that is not closed properly
|
|
|
|