Using C++ for experiment

Jan 20, 2016 at 8:19pm
Hi, I need to get this code to ask for the initial speed, omega. I understand it would need a prinf statement followed by scanf, but do not know what to include in them or what else to do. Also how do I get it to calculate values for the angle theta (in radians) Thank you in advance.

CODE -

#include <stdio.h>
main()

{

int i; // step counter
float theta = 0; // initial value for angle
float omega = 0.2; // initial value for angular speed
float time = 0; // initial time
float dt = 0.01; // time step
int steps = 100; // number of steps

for(i=0 ; i<steps ; i++) {
time = time + dt;
printf("Step number %i Time equals %f\n",i,time);
}
system("PAUSE");
}
Jan 20, 2016 at 8:22pm
Jan 20, 2016 at 8:24pm
try #include<iostream>
an using cin>> to read and cout<< to print by screen
Jan 20, 2016 at 8:25pm
sorry,I wasn't sure whether beginners was the right place to post or not.
Jan 20, 2016 at 8:33pm
M4ri0 E - where would I put the cin>> and cout<< bit, sorry i'm a bit of a beginner at this
Jan 20, 2016 at 8:41pm
just like this:
including <iostream> as a library allows you to use cin and cout
an example would be:

#include <iostream>
using namespace std;

int a,b,sum;

int main(){
cin>>a>>b; //to introduce data by console
sum=a+b;
cout<<sum; //to show te answer
//you can also : cout<<a+b;
}
Jan 20, 2016 at 8:48pm
I tried doing that and an error message came up stating: fatal error: iostream: no such file or directory
Jan 20, 2016 at 8:56pm
what's your IDE and compiler?
Jan 20, 2016 at 9:11pm
why don't you try to use #include<bits/stdc++.h>
Jan 22, 2016 at 7:24am
you use
1
2
#include <iostream>
using namespace std;
for C++

and you use
 
#include <cstdio> 
for C

although iostream should work for C output as well i think they added it or something.
and don't forget your main function needs to return and integer value.

and you want %d for your int not %i.

you can also declare and initialize i in the for loop like for(int i = 0; i <10; i++)
and in that for loop you can increment time the same way you increment i such as time += dt;


are you going to need to convert to radians first? anytime you need to convert to radians or visa versa you just multiply by 180/PI or PI/180, but i think if you don't specify it is done in radians although i could be wrong

one last thing just input a variable at the end instead of a pause. :)
or just type std::cin.get(); thats just waits for the user to hit enter
and is a great way to pause
Last edited on Jan 22, 2016 at 7:42am
Jan 22, 2016 at 7:39am
i just read you want to prompt then input for omega and i didn't see that in your program so ill be consistent with C and show the input since you have the output down just use %d for int.

then input is simular and straight forward
1
2
3
4
5
6
7
//assume prompt
int y;
scanf("%d", &y");
//multiple values
int x, y, z;
float number;
scanf("%d %f", &x, &y, &z, &number");
Jan 28, 2016 at 2:26am
Hi, according to our lecturer we have to have it starting with the #include<stdio.h>
the bits in bold is what I have done, and the rest is copied and pasted from our lab manual.

I needed to modify the code to show the initial speed, omega and modify the program so that it also calculates values for the angle theta (in radians).
Remember in every time step ‘dt’ the angle will increase by an amount equal to
w(greek symbol)dt, so you will need a statement like: theta = theta + omega*dt. Print out the value of the angle too.
The overall angle at the end (theta) should be equal to the angular speed (omega)
multiplied by the total time. Add an if statement that checks and prints out a relevant
message depending on the outcome.

Thank you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
 main()
    {
     int i; // step counter
     float theta = 0; // initial value for angle
     float omega = 0.2; // initial value for angular speed
     float time = 0; // initial time
     float dt = 0.01; // time step
     int steps = 100; // number of steps


     for(i=0 ; i<steps ; i++) {
        time = time + dt;
        printf("Step number %i Time equals %f\n initial speed %f/n",i,time,omega);
     }

      system("PAUSE");
     }
Topic archived. No new replies allowed.