C :: Creating A Pendulum With Gravity

Jan 11, 2014

I have recently begun programming c within my university course. I have been given a task to add arrays to a program I had previously made to make it record the values as I expect I will then quote them on an exported document.

Code:

#include <stdio.h>
#include <math.h>
main() {
int i; // step counter
float theta[10000]; // initial value for angle

[Code]....

The pendulum is meant to swing and take gravity into account, then it is meant to loop and record values for the step count, time, theta and omega (mainly theta and omega required). The boundaries are set so once the angle of theta reaches 180 degrees it then stops. I believe the problem lies in the way I have created my theta array, however, I don't properly know how to implement this. Upon launch it asks for omega, as required, after inputting the value it crashes.

View 1 Replies


ADVERTISEMENT

C++ :: Spaceship Game - Creating Gravity For Each Object Depending On Mass

Mar 27, 2012

I've been having trouble working on this physics engine of mine. Right now I'm having trouble adding gravity.

The game is a spaceship--which you control--flying around in space with asteroids and eventually the ability to shoot bullets. There should be a wrap on the edges of the screen and gravity for each object depending on their mass.

I'm creating a forceX and forceY for each force put onto each object, and then computing that force into a velX and velY which will determine the direction of each object and at which speed.

Where my problem arises:

Code:
//add gravity pulls to forces
for(int i = 0; i <= pushCount; i++) //add gravity pulls for each object {
for(int u = 0 ; u <= pushCount; u++) //each object should add a force for every other object {
if(i != u) {
switch(id[i])

[Code] ....

View 7 Replies View Related

C++ :: Gravity Simulator Implementation

Jul 9, 2013

I've been working on creating a simulator to crash two galaxies together as part of a project to stress test a CUDA super computer. I've got a long way to go and am currently just working on correctly simulating n-body gravity functions. First I will use this to simulate the cores of the galaxies (the black holes) and eventually the stars.

So long story short I'm working on the beginnings of a gravity simulator. At this point I found some basic code that works well but doesn't quite give the effect I'm looking for.

The code below only pulls each object towards each other like a spring faster and faster until they shoot off into infinity. I try to give one of my bodies an initial velocity to get it to orbit another, but it always just shoots straight at the other body. I'm thinking I need to factor in inertia so that the initial velocity doesn't just get calculated away really fast by the other calculations.

I'm really looking for a bit of direction to get a real gravity simulator with orbits and such working right so eventually I can scale it up to a galaxy, throw in 100B stars and let the CUDA run for a month..

Code:
void update_galaxies(GLdouble elapsedTime) {
//Calculate gravity simulations
GLdouble r1, r2, r3;
r1 = r2 = r3 = 0.0;

for(unsigned int i = 0; i < galaxies.size(); i++)

[Code] ....

As you can see, I'm calculating all the bodies in a vector called "galaxies" with each other, and doing a basic gravity calculation to it. The update_position function simply takes the calculated acceleration and uses it to calculate the velocity and position based on the "elapsedTime".

I think I need to use the Varlet or Runge-Kutta integration methods, after doing a bit more research.

View 9 Replies View Related

C++ ::  Creating A New Directory?

May 11, 2013

How do you create a new directory in C++? I would like to do this using the standard library and no external, third party or non standard libraries/headers. I need to do this without calling system() or system("mkdir").

View 7 Replies View Related

C/C++ :: Creating A Static Map

Jun 19, 2014

I have a class where I'm trying to create two static maps. I know I haven't separated the implementation from the declaration. This is how I'm required to program at the moment.

My example class that I'm trying to compile looks like this:

#include <map>
#include <string>
class Foo {

[Code]....

I keep getting compiler errors, though, saying:

invalid use of qualified-name Foo::map1
and
invalid use of qualified-name Foo::map2

I can't initialize these maps explicitly; in my real program they're created by reading in a few different files' data on startup. I need to be able to access the maps throughout my entire application, and I don't want to be required to read in the same data file over and over again.

how static maps can be created properly, or failing that explain another methodology I could use to make them available to my entire application?

View 3 Replies View Related

C/C++ :: Creating Username From Name

Nov 6, 2014

I'm trying to create a simple program that reads in the user's first name, middle name, and last name. Then inside of the userName function, I want it to only take the first letter of firstName, first letter of middleName, and then just output the full last name. So for example: if your name was John Kevin Smith, it would output jksmith. I'm having a lot of compile issues due to the pointers. I have to use the function in this code, so the pointers are needed.

#include <stdio.h>
#include <string.h>
int main() {
char userName (char firstName[12], char middleName[12], char lastName[12], char firstInit[1], char secondInit[1]);
char firstName[12];
char middleName[12];
char lastName[12];
char firstInit[1];

[Code] ....

View 13 Replies View Related

C++ :: Creating INF Data Type?

Jan 21, 2015

I want to create a new data type called an inf_t. It's basically infinity (which for C++ is 1.7e+308). The only reason I want this is because I want to overload the cout << operation to print out INF/inf. Should I do this in a struct?

Code: struct inf_t {
private:
double inf = 1.7e+308;
};
std::ostream& operator << (std::ostream &stream, inf_t inf) {
stream << "INF";
return stream;
}

View 4 Replies View Related

C++ :: Creating New Pointer For A Class

Aug 2, 2013

I'm currently reading the C++ Guide for Dummies

Anyway right now I'm working with pointers and classes, and when I create a new pointer for my class it looks like this...

Pen *Pointerpen = new Pen;

But in the book they threw in this...

Pen *Pointerpen = new Pen();

Can you actually designate memory space for a function? Or was this a typo on their part? It's never come up before and they didn't explain it.

View 5 Replies View Related

C++ :: Creating Do-While Loop In Program

Apr 28, 2014

I need starting a do-while loop in my program that runs up to 10 time that asks the user to enter up to 10 scores, which include the slope and rating for the course, and calculates the handicap for each score.

Here's what I have so far:

Code:
{
//This program calculates a golfers handicap.
#include <iostream>
#include <string>
using namespace std;

[Code] ....

View 1 Replies View Related

C++ :: Creating Own Vector Class?

Nov 15, 2014

I was trying to implement own vector class and wrote some code. Below is the code.

Code: #include<iostream>
#include<vector>
using namespace std;
template <typename T> class MyVector
{
T *mem;
int m_size,final_size;
public:
MyVector() : final_size(1),m_size(4)
{

[code].....

I have question on this function.

Code: myVecPush_back(T t){}

Here I was able to put elements upto any number of time, but I have allocated memory to only 4 elements in T *mem.

My question, why the program is not crashing when I tried to put elements more that 4?

Is since the variable is of type Template? Any specific reason for this?

View 2 Replies View Related

C++ :: Creating Events And Registering To Them

Mar 16, 2014

I want to build a library using c++ which will serve as an abstraction layer between applications and low-level process. The library will provide some APIs for the applications for some purposes.

For example the low-level process, may send an indication to the library i.e. raise an event, and the library in turns send it to all the applications, which have their registered callbacks to this library.

Any example for the previous scenario in c++ using boost library or the standard library will be perfect in Linux environment? The example that I want is: Generate event from a process and pass it to the library, then let an application register callback to the library.

Initially I know in Linux, I may use signal to send events, but my plan is to have a something more general not tighten to a specific OS.

View 1 Replies View Related

C :: Creating Shell In Linux

Feb 22, 2015

This is what so far i did

Code:
#include <stdio.h>#include <string.h>
#include <ctype.h>
#include <bsd/string.h>

int
main(void)

[Code] ....

How to do this Using the fork(), execvp() and waitpid() system calls, launches the requested program and waits until the program has finished.

View 3 Replies View Related

C :: Creating Border Around Program

Mar 6, 2015

I am trying to create a border around my program. It looks like this: . I had to read a file in, which is in the attachment and produce the desired output in the image. I have produced the desired output, but without the border.

Here is my code thus far:

insert Code:
#include<stdio.h>
#include<Windows.h>

#define HEIGHT 21
#define WIDTH 78

int map[HEIGHT][WIDTH];
int i = 0, j = 0;

[Code] ....

View 2 Replies View Related

C :: Creating Rows And Columns

Mar 6, 2015

how to make the it all work later...but in the mean time how can i get this to display this? Note it has to be made using as a console program. The "Description" and "Cost/ib" collums will be referenced through use of a header file. all else is done by user input and calculations.

View 2 Replies View Related

C :: Creating A Library Function

Jan 27, 2015

I want to create a C library function that i can directly call in my code from any .c file having main program.following are codes...code of library function "foo.c"

Code:

#include "foo.h"
int foo(int x) /* Function definition */ {
return x + 5;
} header file "foo.h"

Code:

#ifndef FOO_H_ /* Include guard */
#define FOO_H_
int foo(int x); /* An example function declaration */
}

[code]....

to use this i have to compile the file in below manner...

Code: gcc -o my_app main.c foo.c

My concern here is that i want to compile the main.c and use function without compiling foo.c with i.e.

Code: gcc -o my_app main.c

any user of this function should only compile his program and should be able to use the function, the foo.c file should remain hidden from him

my system is Linux 2.6.18-308.4.1.el5 #1 SMP Wed Mar 28 01:54:56 EDT 2012 x86_64 x86_64 x86_64 GNU/Linux

View 9 Replies View Related

C :: Creating Dynamic String?

Mar 6, 2015

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
char * form(char *format, ...);
}

[code]...

So, this is the code i have problem with, as far as i can see, function form actually does the same thing that printf does.It probably puts all of the arguments sent to the function together into one string.

View 4 Replies View Related

C++ :: Creating Game Using SFML

Apr 26, 2014

I'm currently working on a 2D space shooter game in C++ using SFML library. What I need to know is how make an object (ex:laser) fire up from object (ex:player) when a user press button??

View 1 Replies View Related

C++ :: Creating STL Queue Of Arrays?

Dec 3, 2013

Is there any possible way to create an STL queue of arrays? I tried

queue<int[]>q;

It didn't work?

View 2 Replies View Related

C++ :: Creating A Function In A Class?

Feb 16, 2014

I am currently working on a "bag" class which is sort of a common sense answer to creating a random class with difference functions. I am attempting to create a "union" function which takes two bags ie: bag1 and bag2, adds all the items in both bags and creates a new bag ie: "bag3". For some reason I keep coming up with problems instead of solutions. Maybe it's the fact I just got done with 2 days of calculus. I don't know. My code is below. Both a main(source) and header file.

Header

#ifndef BAG_H
#define BAG_H
const int BAG_CAPACITY = 20;
template <typename T>
class Bag {
private:
int count; // Number of items in the Bag

[code]....

View 1 Replies View Related

C++ :: Creating Exact Age Calculator?

Aug 3, 2014

I've been wondering this, since there is so many things which needs to be taken care of. I created one, but it wasn't precise.

View 19 Replies View Related

C++ :: Creating A Custom Filetype?

May 21, 2013

I am making a level editor that needs to output a custom map file for use with source code that I will write for a graphics library.

My question is: What do I need to bear in mind when deciding how to structure a custom file type? Also how can I encode a standard bitmap image into my file (the tile set) so that it can all be contained in a single file rather two files; the map file and tile set (.bmp file).

View 3 Replies View Related

C/C++ :: Creating Gradebook In A Table

Nov 6, 2014

I'm unable to get the grades to print in the table format, it only takes the homework grades and not the tests and quizes.

// week 8.cpp : Defines the entry point for the console application.
// Gradebook 1.cpp :

/*Rewrite the program "Gradebook 1" to contain 1-dimensional arrays. All information entered or calculated within the program is located in an array. The final output(Class Summary) should contain a summary sheet that contains all of the information in table form. */

#include "stdafx.h"
#include <stdio.h>
#define Size 30//Maximum class size
#define max 100//maximum grade
#define min 0//minimum grade

[Code] .....

This is my result :

Welcome to the Gradebook Program.

Please enter the number of students: 2
Enter the number of Tests to be averaged: 2
Enter the number of Quizzes to be averaged: 2
Enter the number of Homework assignments to be averaged: 2

[Code] .....

View 4 Replies View Related

C# :: Creating A Magic Square

Feb 5, 2015

My university assignment requires us to create a magic square. The definition of which can be found on Wikipedia and such, but essentially it's a (n) times (n) grid where all the row, column and diagonal totals meet the formula [n(n2 + 1)]/2. So a 3x3 would provide totals of 15 for example.

It's been requested that we use brute force for this, nothing mathematically fancy. I've succeeded in the task and initially during testing was coming up with a result of a 3x3 magic square after between 10 million and 1 billion attempts over a few minutes to nearly an hour.

I proceeded to refine my code and have as little as possible in the while loop which I'd break out of once the function had found a magic square, nothing really made much difference until I moved the declaration of my random to just before my while loop instead of within.

Random RanGenerator = new Random();

Once I'd done this I was getting 3x3 magic squares after a few thousand iterations in less than a second, which has completely baffled me. I can understand if it would save time, but how could that reduce the amount of attempts required to solve the magic square?

View 6 Replies View Related

C/C++ :: Creating A Singleton Pointer?

Jun 26, 2014

I have a class in my application that only needs to be created once, but the object needs to be available to all other classes in my application if necessary. Since declaring everything static can be restrictive (as I understand), I created a class like this:

class Foo {
// Data members
// Constructor/Destructor
// Functions
};
extern Foo* myFoo = new Foo();

And then the global variable gets deleted at the very end of the main method when everything is done:

#include "Foo.cpp" // (yes, I know this is normally bad, this is how I'm required to code)
int main() {
// do stuff
delete myFoo;
return 0;
}

These won't link, though, because I get undefined reference linking errors to myFoo wherever I use it. I'm pretty sure this means I'm creating a singleton wrong, but I'm not sure what I'm doing wrong -- there's no const conflicts and the pointer is properly initialized (to my understanding). If there's a better way to do this than extern, I'm completely open to it, as long as it's understandable and works.

View 9 Replies View Related

C# :: Creating A Reminder System?

Apr 26, 2014

I wanted create a Reminder Systems for my Project

So Scenario is like this,A Gym has owned a Software to maintain record of employee and customer, everything is ready now only reminder system is to be made. Reminder system is like for example if customer comes to gym and inquires about plan and fees and says call back him at so and so time, so receptionist will require reminder system to alert him at the time given by customer for call back.

What i have done

1.Created a Listview to show reminder list, and created two button ADD reminder and DELETE reminder When reminder is added following things are added in Reminder table in Database

i.ID
ii.Title
iii.Message
iv.Time
v.Date

2.when i display the reminder table in reminder listview, the order will be in (Date, time)asecnding order.

---Till here everything is done---

Now i am confused here, lets take example The first Reminder in Listview is around 3.00 clock, when the time is reached i want to show message that "call back this customer".

So to do this i was thinking of using timer and on each tick compare Current time with Reminder time, if equals display message, but this method is inefficient it can freeze my UI.

The Alternate way i was thinking to run this timer inside Background worker as it would create a different thread there will be no issue with my application.

View 5 Replies View Related

C++ :: Creating And Terminating Threads?

Aug 31, 2014

I had a requirement where i needed to create a thread and if the execution of thread is not completed in 5 minutes i needed to terminate its execution and continue with other part of the code.

I used the below code to create the thread

_beginthread(FuncnCall,0,NULL);

HANDLE hThread = GetCurrentThread();

Then after this code, I used the below code to check for 5 minutes

for (int i=1;i<=0;i++) {
printf("Value of i=%d
",i);
if(threadFinished) {
break;
} else {
Sleep(1000);
}
}

After this if the value of "threadFinished" is false then i am terminating the thread like below

if(threadFinished == false) {
TerminateThread(hThread,0);
CloseHandle(hThread);
}

The Problem here is, after terminating the thread, the program abruptly closes by giving fatal error. Looks like memory leakage is happening after terminating the thread. Is it not the right way to safely exit the thread?

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved