C/C++ :: Pseudo Code Implementation Using Three Semaphores
Apr 27, 2014
Write a program to implement the following Pseudo Code using three semaphores.
Here is the pseudo code:
int sizeofbuffer = 10
semaphore Access = 1, Item = 0, EmptySpace = sizeofbuffer;
int nNumberOfItem = 0;
void Farmer() {
while ( true ){
semWait( EmptySpace );
[Code] .....
View 3 Replies
ADVERTISEMENT
Apr 15, 2013
I have assignment which requires me to print out and array code and a pseudo code. I dont no what a pseudo code is,.
View 2 Replies
View Related
Sep 12, 2014
I'm doing a practice problem where I need to read pseudo code and understand what it does.
For this code, I believe it will move the robot forward while i<650, so it moves it forward for 650 steps in total. Then it sets the motors to 0 when it's finished.
Code:
while(i<650)
{ rotateCW(2);
rotateCCW(1);
Delay1KTCYx(50);
i++;
LATD=0x00; //Set all Port D to 0
}
This code I'm having the most trouble with. So while(1) the pot(potentiometer) read's the ADC. Then it scales the adc, not 100% sure what the +20 and *.22 mean, I think it has to do with binary conversions?
Now it sets pin 1 of port 1 to 0, delays based on pot above, and sets it back to 1, and delays again. I assume something to do with controlling motor speed based on the potentiometer.
Code:
void main(void){TRISD = 0x00; // Setting port D to outputs
LATDbits.LATD0 = 1; // Setting bit 0 of port D to 1
while(1) {
pot = readADC();
pot = pot * 0.2297 + 20;
[Code] ......
My best guess is: The car moves forward 400 steps, stop, turn right for 230 steps, stop. At this point it would begin moving straight, and it would turn left/right as needed to keep the robot in the center of the track.
Code:
void main(void) {
for(i=0;i<400;i++) {
rotateCW(2,motor); //Rotate motor two (one step)
rotateCCW(1,motor); //Rotate motor one (one step)
Delay1KTCYx(50);
[Code].....
View 5 Replies
View Related
Jan 3, 2015
I want to create a top trumps game using pseudo code with variables, loops, If statements and inputs, how would I write it?
View 3 Replies
View Related
Apr 15, 2013
I have finished writing the code for the following program. I have concerns with the pseudo-code that I have written and the flow chart seems really complicated. I dont know how to build it. Here is the question:
An Internet service provider has three different subscription packages for its customers:
Package A: For $9.95 per month 10 hours of access are provided. Additional hours are $2.00 per hour.
Package B: For $14.95 per month 20 hours of access are provided. Additional hours are $1.00 per hour.
Package C: For $19.95 per month unlimited access is provided.
Write a program that calculates a customer s monthly bill. It should ask which package the customer has purchased and how many hours were used. It should then display the total amount due.
Input Validation: Be sure the user only selects package A, B, or C. Also, the number of hours used in a month cannot exceed 744
Here is my program:
#include <iostream>
using namespace std;
int main() {
char package;
int hours;
[code]....
What should be my pseudocode and flowchart ? Can i copy and paste my pseudocode here ?
View 2 Replies
View Related
Jun 29, 2014
What is the program of this pseudo code?
Set found to false.
Set position to -1.
Set index to 0.
While found is false and index < number of elements
If list[index] is equal to search value
found = true.
position = index.
End If
Add 1 to index.
End While.
Return position
View 5 Replies
View Related
Feb 20, 2014
I have made a program that converts or reverses an input string. Here is my code working fine
static void Main(string[] args){
string input = Console.ReadLine();
string[] words = input.Split(' ')
for (int i = words.Length; i > 0; i--)
Console.Write(words[i - 1] + " ");
Console.WriteLine();
Console.ReadLine();
}
How can I write this program in pseudo code?
View 2 Replies
View Related
Oct 26, 2014
Design an algorithm using flowchart or pseudo-code to prompt the user for a series of positive integer values. Calculate the average of these values and display the average at the end of the program. Assume that the user types the sentinel value -1 to indicate end of data entry.
Sample input-output:
Enter a positive integer (-1 to end) : 5
Enter a positive integer (-1 to end) : 7
Enter a positive integer (-1 to end) : 6
Enter a positive integer (-1 to end) : -1
The average value is: 6
I searched online and found out this solution however it is only for three numbers.Is there any way of modifying this to include the sum of x numbers / number of x(integers) to find the average?
View 5 Replies
View Related
Feb 27, 2014
I have come across below code to demenostrate the use of semaphores
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#define NITER 1000000
int count = 0;
void * ThreadAdd(void * a)
[Code] ....
Why the question of syncronising threads and mutexes and semaphores come into the picture when we join threads?
Like in the above example we have joined the first thread and then second thread so that main process will pause for the first thread to finish as its joined and then main process resumes and gives control to second thread and pauses till its finished as this thread is also joined.
Then where is question of simultaneous access?
First thread does its job and ends itself.
Second thread does its job and ends itself.
I wonder why there is need to use semaphores or mutexes in this example? I am very new to Multithreading and this question bothers me as to why we have to worry about simeltaneous access when we can join threads and can make control wait till that thread ends?
View 10 Replies
View Related
Mar 2, 2014
I'm trying to use semaphores on three different processes so that each process won't enter the critical region at the same time and also go in the order: process 1 -> process 2 -> process 3. However, everytime i run the code the process 3 keeps going before process 2.
Here's my code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
[Code].....
the processes should print out like this:
process 1: 100000
process 2: 300000
process 3: 600000
View 1 Replies
View Related
Apr 26, 2014
I have a problem with my C#. Trying to create the airstrip with planes heading out of the hangars and on to the road to the airstrip. Each Hangar pannel is connected with closes road to them. What i am stuck with right now is connecting the roads together so each one of them will see if the next road is free and if it is it will send the plane (colorder square for now) to it and then proceed to the next one when finaly they will line up for the final panel(airstrip) and take off.So far they are just going down the each road they are assigned with semaphore and thread.
using System;
using System.Windows.Forms;
using System.Threading;
using System.ComponentModel;
using System.Collections;
using System.Data;
using System.Drawing;
[Code] ....
Connecting the "p2" with "p5" so the graphics from "p1" the square will travel through P2 and P5 this would be great and will allow me to procede with my studies.
Also i do realise that this might not be the best ways of using concurrent Csharp and not the up to date one but this is what i have been assigned with and do not have any other options unfortunately.
How i understand this is that i need to have 3 buffers and 3 semaphores for each panel. But i do not know how to add those to each one of them and then connect it with the next one as if i try and add them it either puts a cross in the design view over that panel or says that there is limited options for this statement.
This is the way that i want to do it unfortunately it must be so. The most flexible this can be is by adding an array for semaphores and buffer ( do not know how to do that either).
View 7 Replies
View Related
Feb 6, 2013
How can I generate a pseudo-random real number from interval [0,1] ?
Can it be generalized to any interval? Like [0,a], where 'a' is a parameter?
I tried searching for it, I only found rand(), srand(), random(1), and randomize. None of it actually seems to work for me..
Later I actually succeeded with something like
srand( (unsigned)time( NULL ) );
printf( " %6d
", rand() );
but it only produces up to five digits integers and I cannot divide by 99999 to get it into [0,1].
View 11 Replies
View Related
Aug 18, 2013
I have to develop minimalistic implementation of RSA algorithm in C for an embedded device.
I'm doing that for two days but I have run into a problem. The N modulus is the limitation for the maximum message value to be encrypted with RSA.
For example theoretically RSA-1024 can encrypt/decrypt messages 1024 bits long but I still cannot understand how to choose p and q values to produce N == pow(2, 1024).
Is it possible to encrypt/decrypt 1024 bits long messages in practice if the N < pow(2, 1024)?
So far I'm getting the following results
Code:
Encrypting with RSA
d=15689981, e=21, n=16484947
In=16484942, Encrypted= 6074492, Out=16484942 (OK)
In=16484943, Encrypted= 5468920, Out=16484943 (OK)
[Code] ....
View 10 Replies
View Related
Aug 8, 2013
I was trying to implement Big Integer Implementation. I wanted to start with the basic operation of addition. I am having some problems with operator overloading part
/**
BigInteger implementation
*/
#include "conio.h"
#include "iostream"
[Code]....
View 9 Replies
View Related
Apr 19, 2014
I was looking at this tutorial: [URL] ..... And I was wondering if implementing it in MVC would be pretty much the same way? How would I display feed items in the views page using?
I tried something like:
ReaderModel Reader = new ReaderModel();
Collection<Rss.Item> List;
List = Reader.GetFeed();
ViewData["RssItems"] = List;
// then in index.cshtml
@foreach(Collection<Rss.Item> items in ViewData["RssItems"]) {
<h3>items.Title</h3>
...
}
I don't think this is right as I'm getting those red error lines...
View 3 Replies
View Related
Aug 5, 2013
Here is the code,
Code:
class A {
private:
void* operator new(size_t size);
};
int main() {
return 0;
}
The code above compiles fine without errors. But operator new might not have implementation body?
View 3 Replies
View Related
Apr 13, 2014
I have a class 'A' which is almost perfect for my needs. Class 'B' uses class 'A' I've now designed Class 'C' and Class 'D' and noticed that there is a good chunk of code in class 'B', 'C' and 'D' for using Class 'A' is duplicated. I've separated out this code in specific, standalone functions in each of the classes. Now I'm wondering where this code should go. At the moment, the functions are duplicated in the three calling classes (B, C and D). Placing the functions into class 'A' would break the single responsibility principle. Inheritance to add functionality would likely break both SRP and LSP. The one that seems that it may work is composition.
However, Is designing a complete class just for a few functions over kill?
Would it be valid for classes 'B', 'C' and 'D' to access both the new class 'E' (which would depend on A) and the old class 'A' (which would have to be the same instance as the instance in the new class 'E'), or should the new class 'E' provide sufficient functionality so that Classes B, C and D don't need to access Class A directly? It would seem that its then an incomplete interface of the original object with additional functionality (ie, incompatible) Or should I do it a completely different way?
View 4 Replies
View Related
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
Mar 12, 2013
I'm having problems with implementing depth first search.
Code:
6 10 //6vertices 10edges
0 2 //vertex and its adjacent vertex
1 0
1 2
2 3
2 4
3 1
4 1
4 3
4 5
5 3
Output to terminal: 0 2 3 1 4 5
but it should be: 0 2 4 5 3 1
Here's my code:
#include<stdio.h>
#include<assert.h>
/* maxVertices represents maximum number of vertices that can be present in the graph. */
#define maxVertices 100
void Dfs(int graph[][maxVertices], int *size, int presentVertex,int *visited)
[Code] ....
View 1 Replies
View Related
May 31, 2014
Code:
#include <stdio.h>#include <unistd.h>
#include <stdlib.h>
#define STACKSIZE 100
struct stackk
{
int top;
int items[STACKSIZE];
};
typedef struct stackk *s;
[Code]...
View 1 Replies
View Related
Apr 7, 2014
I am trying to implement a stack class which has struct data type as its private member. I am getiing the following error ,
bash-3.2$ g++ stack_str_arr.cpp
stack_str_arr.cpp: In member function ‘void stack::push(int)’:
stack_str_arr.cpp:39: error: ‘top’ was not declared in this scope
stack_str_arr.cpp: At global scope:
stack_str_arr.cpp:43: error: no ‘void stack::display()’ member function declared in class ‘stack’
Code:
#include<iostream>
using namespace std;
#define MAX 5
class stack {
public :
stack();
[Code] ....
View 2 Replies
View Related
Sep 19, 2013
I'm implementing a 4x4 matrix class and all is going well until the inverse function turned up. I'm trying to implement the inverse function, but I can't seem to get my head around it.
I've tried the internet, but found nothing useful. Also, I've looked into source code of other programs/libraries that implement a matrix class, but the code is unreadable.
How I can implement this damn 4x4 inverse function? I know the process of inversion, but putting that process into code is proving quite a challenge.
In addition, I do have some code, but it's unmanageable and inefficient at the moment. If you want to see it, just ask.
Additional question(s): What applications does the inverse matrix have in 3-D?
View 3 Replies
View Related
Nov 22, 2014
I'd like te have a heap which contains any item inserted by me. However, when I insert the values, if I delete the min value of this coded heap twice, I get the min value uncorrect.I could not find where the mistake is.
Code:
void BinaryHeap::percolateDown(int hole) {
int child = hole*2;
while (child < size) {
[Code].....
View 1 Replies
View Related
Sep 12, 2014
I am trying to implement some kind of named class. It would look something like this:
class MyClass {
virtual std::string getName() = 0;
};
And now (what doesn't pass the compilation)
template <std::string NAME> class MyNamedClass {
std::string getName() { return NAME;}
};
And so every time I would like to have a class with a name, I could just do the following:
FinalClass : public MyNamedClass<"FinalClass">{};
The idea is not to have to always reimplement getName(), and just do it concisely in the declaration.
View 7 Replies
View Related
Aug 26, 2013
I was trying to implement a hash function in c++. This is just for learning purposes and not for a class project or assignment question. I had some questions before I started programming it:
1) Is it meaningful to have a hash function which maps string to string, string to int, int to int, float to int?
2) Can we have a template implementation which does element to element hashing?
I looked at several sources for a clear understanding of concepts and implementation technique but could not find a good source for reading about hashing.
View 3 Replies
View Related
May 11, 2013
I am currently trying to implement a pathfinding algorithm using c++ and opengl. My code so far is shown below:
#define OPEN 1
#define CLOSED 2
#define UNVISITED 3
#define BLOCKED 4
#define HIGHLIGHT 5
#define ONROUTE 6
#define GOAL 7
#define GSIZE 20 // size of tile grid
#define ISTART 15 // index position of starting tile in grid
[Code] ....
View 2 Replies
View Related