Jump to content

Computer Programmers


OUMike

Recommended Posts

Posted

Okay guys, I need some help. I had to write a 3 part program from my C++ class at UH and I'm having some trouble. For some reason I'm getting a warning on the function call, and I'm not entirely sure how to write the 3rd part of the program. The 3rd part wants me to write a function that takes a string (inputted by user) and counts the words. I have no freaking clue how to start it. I know I'm probably reaching for help, but I figured why not. Theres bound to be some folks with some C++ know how on here. I'll put the exact instructions from the HW followed by my code what I have so far. Thanks guys.

 

For the Count–Words option, write a C++ program that moves linearly through char data in an array b[ ] until a '\n' is reached. The program should count the total number of words in a sentence— typed in by the user—from keyboard and should output the result. A word is defined to be any contiguous group of ASCII chars that start with letters A through Z (upper case ) that don't contain white space. Words are separated by white space - e.g. blanks, or tabs. Of course the last word could be terminated by the '\n' character. For example, if the array b[] contains: the Dog At5674 654 Mypaper'\n', your program should output: Total word count: 3. Words are: Dog, At5674, Mypaper.

 

And now my code..

 

#include <iostream>

#include <iomanip>

#include <string>

#include <cstdlib>

using namespace std;

//void CountWords (

void Erase (int a[], int *ptrNum, int *ptrSearchElement);

void RevWordsInPlace (char array[]);

 

 

int main()

{

char Sel;

 

cout << "Welcome to Homework #5" << endl;

cout << "To start please select from the following menu:" << endl;

cout << endl;

cout << "[E]rase-Array Content Count-[W]ords [R]ev-Words [Q]uit" << endl;

cin >> Sel;

 

switch (Sel)

{

case 'e':

case 'E':

Erase;

break;

case 'w':

case 'W':

//CountWords << endl;

break;

case 'r':

case 'R':

RevWordsInPlace;

break;

case 'q':

case 'Q':

cout << "Thanks for using!!" << endl;

break;

default:

cout << "You have entered an invalid option!" << endl;

break;

}//end of switch statement

}//end of main

 

 

void Erase (int a[], int *ptrNum, int *ptrSearchElement)

{

int x, y;

ptrNum = new int [x];

ptrSearchElement = new int [y];

 

cout << "How many integer numbers would you like to use?: " ;

cin >> x;

cout << endl;

 

cout << "Please enter your interger values now: " ;

cin >> a[x];

cout << endl;

 

cout << "Which value would you like to remove?" ;

cin >> y;

 

cout << a[x] << endl;

 

}//end of Erase

 

void RevWordsInPlace(char array[])

{

char x, y;

 

cout << "Please Enter Size of Array: " ;

cin >> x;

cout << "Please enter your string(s) now: " << endl;

cin >> array[x];

cout << array[x] << endl;

 

if (y=0, y<x, y--)

cout << array[y-x];

}

 

/*void CountWords (

{

 

} */

 

word wrap kinda screwed it up too...sorry.

Posted

The compiler is complaining because when you call the Erase and RevWordsInPlace functions, you're not calling them as a function.

 

For example, to call Erase, you would need to change the line in your switch to something like

 

Erase(param1, param2, param3);

 

But change the parameters to your variables instead.

 

Also, at that point, your array will be empty, so you'll need to make sure that the user can enter an array before selecting 'E'. You might put the whole thing in a for/while loop, and only exit the loop once the user has chosen Q.

 

As for the third part of your program, loop through the array and keep track of each time the current character is a valid character to start a word with and was preceded by white space.

 

Hope that was enough to help get you started :crackup:

Posted

alright

 

switch (Sel)

{

case 'e':

case 'E':

Erase;

break;

case 'w':

case 'W':

//CountWords << endl;

 

 

Now take a good look at your code, you are suppose to write C++ not Qbasic :crackup:

 

you can't call functing like this Erase;

 

it should be Erase(para1, para2, etc..);

 

Please follow Chris's Instruction...

Posted

Hi Chris:

 

If you get a chance please look at my code and let me know where I can improve or optimize - this program control my Christmas Lights - I already build and ran last Christmas..

 

thanks

 

uj. :crackup:

 

/* compile this code by using g++ -Wall -I. -D__STL_USE_NEW_IOSTREAMS -O2 -o clights clights.cpp

* this programm control 2006 Christmas Lights

* written by UJ - 12/07/2006

* set crontab file as per below:

0 17 * * * /home/upali/cclights/lights_on >& /dev/null

30 23 * * * /home/upali/cclights/210_on >& /dev/null

30 7 * * * /home/upali/cclights/lights_off >& /dev/null

 

*

* */

 

#include <sys/io.h> // libc5 ioperm

#include <unistd.h> // glibc ioperm

#include <stdio.h> // u need this for printf

#include <cstdlib>

#include <ctime>

#include <iostream> // new way to use iostream.h

#include <string> // use for chArray

 

/* headers must put in this order, else won't work */

 

using namespace std; /* this also req order to compile */

 

#define PORTADDRESS 0x378 /* Enter Your Port Address Here 378 is my LPT1*/

// #define IRQ 7 /* IRQ Here */

 

#define DATA PORTADDRESS+0

#define STATUS PORTADDRESS+1 // value reported was 63=zero cross and 127 for none zero cross

#define CONTROL PORTADDRESS+2

 

class Channel {

public:

Channel(); // constructor

~Channel(); // distructor

void turnOff();

void turnOn(int step, int duration);

void upload();

void ranDom(int induch);

void shooTingStar(int shoot);

void dimShaker();

void nightRider();

void stepUpDown();

void zeroCross();

void waVe();

void waVe2(int step, int offch);

void test();

 

string chArray ; // channel condition

int MaxLevel; // max light level

int MinLevel; // min light level

int Delay; // how much delay between firing

};

 

Channel::Channel() {

// cout << "Constructor of Channel\n";

}

 

Channel::~Channel() {

cout << "Programm ended\n";

}

 

void Channel::turnOff() {

cout << "Lights show Off\n";

}

 

void Channel::turnOn(int step, int duration) {

// cout << "turning Lights On\n";

int i=0, j, k, bitpattern, run, pinten_s=0;

for (run=0; run<step; run++) {

for (i=0; i<255; ) {

i = i + duration;

for (j=0; j<8; j++) {

bitpattern = i;

if (bitpattern < MaxLevel) {

bitpattern = MaxLevel;

}

for (k=0; k<8; k++) {

loop1:

pinten_s = inb(STATUS); // check LPT pin-10

pinten_s = pinten_s >> 6;

// printf("\n Pin 10: %d",(data & 0x40)/0x40);

if (pinten_s != 1) { goto loop1; } // checking AC 0 cross

outb((bitpattern & 128) / 128, DATA);

outb(2, CONTROL); /* clock hi */

outb(3, CONTROL); /* clock lo */

bitpattern = (bitpattern * 2) & 255;

}

}

outb(1, CONTROL); /* latch hi */

outb(3, CONTROL); /* latch lo */

usleep(Channel::Delay); // 100 ms = 100000

}

}

}

 

void Channel::upload() {

int arrsize, i, j, k, bitpattern, count = 0;

arrsize = Channel::chArray.size(); // find how big is the chArray

for (i=0; i<arrsize / 8; i++) { // BitArray hold 96 bits / 8 become 12 steps loop

for (j=0; j<8; j++) {

 

bitpattern = Channel::chArray[count]; // when i loop above run 12 times * j run 8 each turn = 96

count++;

if (bitpattern == '1') {

// check and set light level

bitpattern = 0;

}

else {

bitpattern = 255;

}

 

for (k=0; k<8; k++) {

outb((bitpattern & 128) / 128, DATA);

outb(2, CONTROL);

outb(3, CONTROL);

bitpattern = (bitpattern * 2) & 255;

}

}

outb(1, CONTROL);

outb(3, CONTROL);

usleep(Channel::Delay);

}

}

 

void Channel::ranDom(int induch) {

srand((unsigned)time(0));

int random_integer=0, bitpattern=0, j=0, k=0, i;

 

// don't work - srand((unsigned)time(0));

for (i=0; i<50; i++) {

srand((unsigned)time(0));

random_integer = (rand()%240)+1;

for (j=0; j<8; j++) {

bitpattern = random_integer;

for (k=0; k<8; k++) {

outb((bitpattern & 128) / 128, DATA);

outb(2, CONTROL);

outb(3, CONTROL);

bitpattern = (bitpattern * 2) & 255;

}

if (induch == 1) { // induch 1 = Latch each channel one by one

outb(1, CONTROL);

outb(3, CONTROL);

usleep(Channel::Delay);

srand((unsigned)time(0));

random_integer = (rand()%254)+1;

bitpattern = random_integer; // each channel will have different light level

}

}

if (induch == 0) { // induch 0 = Latch all channels at once

outb(1, CONTROL);

outb(3, CONTROL);

usleep(Channel::Delay);

}

}

}

 

void Channel::shooTingStar(int shoot) {

int bitpattern=0, counter=0, j=0, k=0;

 

do {

for (j=0; j<8; j++) {

bitpattern = 180; // stay dimm

for (k=0; k<8; k++) {

outb((bitpattern & 128) / 128, DATA);

outb(2, CONTROL);

outb(3, CONTROL);

bitpattern = (bitpattern * 2) & 255;

}

}

outb(1, CONTROL);

outb(3, CONTROL);

usleep(Channel::Delay);

 

for (j=0; j<8; j++) {

bitpattern = 0; // shoot effect

for (k=0; k<8; k++) {

outb((bitpattern & 128) / 128, DATA);

outb(2, CONTROL);

outb(3, CONTROL);

bitpattern = (bitpattern * 2) & 255;

}

}

outb(1, CONTROL);

outb(3, CONTROL);

usleep(1000);

counter++;

} while (counter <= shoot);

}

 

 

void Channel::dimShaker() {

int bitpattern, i, j, k, r, ab;

int bitArray[2];

 

srand((unsigned)time(0));

bitArray[0] = (rand()%240)+1;

bitArray[1] = (rand()%155)+1;

bitArray[2] = 210;

 

for (r=0; r<2; r++) {

for (i=255; i>bitArray[r]; i--) {

for (j=0; j<8; j++) {

bitpattern = i;

for (k=0; k<8; k++) {

outb((bitpattern & 128) / 128, DATA);

outb(2, CONTROL);

outb(3, CONTROL);

bitpattern = (bitpattern * 2) & 255;

}

}

outb(1, CONTROL);

outb(3, CONTROL);

usleep(10000);

}

}

 

ab = (rand()%254)+1;

if (ab < 210) { ab = 210; }

 

for (i=0; i<ab; i++) {

for (j=0; j<8; j++) {

bitpattern = i;

for (k=0; k<8; k++) {

outb((bitpattern & 128) / 128, DATA);

outb(2, CONTROL);

outb(3, CONTROL);

bitpattern = (bitpattern * 2) & 255;

}

}

outb(1, CONTROL);

outb(3, CONTROL);

usleep(10000);

}

 

for (i=0; i<8; i++) {

bitpattern = ab;

for (k=0; k<8; k++) {

outb((bitpattern & 128) / 128, DATA);

outb(2, CONTROL);

outb(3, CONTROL);

bitpattern = (bitpattern * 2) & 255;

}

}

outb(1, CONTROL);

outb(3, CONTROL);

sleep(1);

}

 

void Channel::nightRider() {

int i, j, k, bitpattern;

int bitArray[8]= { 0, 255, 255, 255, 255, 255, 255, 255 };

 

for (i=0; i<8; i++) {

for (j=0; j<8; j++) {

bitpattern = bitArray[j];

// cout << "bitArray J is: " << bitArray[j] << " " << j << endl;

for (k=0; k<8; k++) {

outb((bitpattern & 128) / 128, DATA);

outb(2, CONTROL);

outb(3, CONTROL);

bitpattern = (bitpattern * 2) & 255;

}

outb(1, CONTROL);

outb(3, CONTROL);

usleep(25000);

}

bitArray[i+1] = 0;

bitArray = 255;

}

 

// reverse

for (i=8; i>0; i--) {

bitArray[i-1] = 0;

bitArray = 255;

 

for (j=8; j>0; j--) {

bitpattern = bitArray[j];

for (k=0; k<8; k++) {

outb((bitpattern & 128) / 128, DATA);

outb(2, CONTROL);

outb(3, CONTROL);

bitpattern = (bitpattern * 2) & 255;

}

}

outb(1, CONTROL);

outb(3, CONTROL);

usleep(30000);

}

}

 

void Channel::stepUpDown() {

int i, j, k, bitpattern;

int bitArray[8];

 

bitArray[0] = 250;

bitArray[1] = 220;

bitArray[2] = 200;

bitArray[3] = 180;

bitArray[4] = 140;

bitArray[5] = 120;

bitArray[6] = 80;

bitArray[7] = 0;

 

for (i=0; i<8; i++) {

for (j=0; j<8; j++) {

bitpattern = bitArray;

for (k=0; k<8; k++) {

outb((bitpattern & 128) / 128, DATA);

outb(2, CONTROL);

outb(3, CONTROL);

bitpattern = (bitpattern * 2) & 255;

}

}

outb(1, CONTROL);

outb(3, CONTROL);

usleep(Channel::Delay);

}

}

 

void Channel::zeroCross() {

int i, k, bitpattern, pinten_s;

 

for (i=0; i<8; i++) {

pinten_s = inb(STATUS); // check LPT pin-10

pinten_s = pinten_s >> 6;

if (pinten_s == 0) { pinten_s = 255; }

bitpattern = pinten_s;

for (k=0; k<8; k++) {

outb((bitpattern & 128) / 128, DATA);

outb(2, CONTROL);

outb(3, CONTROL);

bitpattern = (bitpattern * 2) & 255;

}

}

outb(1, CONTROL);

outb(3, CONTROL);

usleep(Channel::Delay);

}

 

void Channel::waVe() {

int i, j, k, asize, bitpattern, count=0, counti=0;

int bitArray[8] = { 210, 200, 190, 180, 150, 128, 120, 0 };

string sarray = "100100101001001010010010100100101001001010010010100100101001001001001001010

10010100100101001001010010010100100101001001010010010010010000100100001001000010

10000100100001001000010010000100100";

 

asize = sarray.size();

for (i=0; i<asize / 8; i++) {

for (j=0; j<8; j++) {

bitpattern = sarray[count];

count++;

if (bitpattern == '1') { bitpattern = bitArray[counti]; }

if (bitpattern == '0') { bitpattern = 220; }

for (k=0; k<8; k++) {

outb((bitpattern & 128) / 128, DATA);

outb(2, CONTROL);

outb(3, CONTROL);

bitpattern = (bitpattern * 2) & 255;

}

}

outb(1, CONTROL);

outb(3, CONTROL);

usleep(Channel::Delay);

counti++;

if (counti >= 8) { counti = 0; }

}

}

 

void Channel::waVe2(int step, int offch) {

// this function can fade each channel 1 at a time

int i, j, k, l, m, asize, bitpattern, count=0;

int bitArray[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };

asize = Channel::chArray.size();

 

for (i=0; i<asize / 8; i++) {

for (j=0; j<8; j++) { // read sarray and set the bitArray pattern

bitpattern = Channel::chArray[count];

if (bitpattern == '1') { bitArray[j] = 1; }

if (bitpattern == '0') { bitArray[j] = 0; }

count++;

}

for (k=0; k<255; ) { // dimm level

for (l=0; l<8; l++) { // for 8 channels

if ( bitArray[l] == 1) {

// channel to be on/dimm

bitpattern = k;

}

else {

bitpattern = offch;

}

// test

 

// cout << "Bitpattern " << bitpattern << " count: " << count << endl;

// sleep(1);

 

for (m=0; m<8; m++) {

outb((bitpattern & 128) / 128, DATA);

outb(2, CONTROL);

outb(3, CONTROL);

bitpattern = (bitpattern * 2) & 255;

}

}

outb(1, CONTROL);

outb(3, CONTROL);

usleep(Channel::Delay);

k = k + step;

}

}

}

 

void Channel::test() {

cout << "Delay Condition A1/B1: " << Channel::Delay << endl;

// sleep(50);

}

 

 

int main() {

 

int i, j, step=0;

 

if (ioperm(PORTADDRESS, 3, 1)) {perror("ioperm"); exit(1);} // set the port access

 

Channel A1; // create channel object A1

 

while(1) {

 

for (i=0; i<50; i++) {

A1.Delay = 10;

step = 2;

A1.chArray = "100100100100100100100100";

if (i > 5) { A1.Delay = 0; step = 5; }

if (i > 10) { A1.Delay = 500; step = 10; }

if (i > 20) { A1.Delay = 100; step = 20; }

if (i > 40) { A1.Delay = 5; step = 20; }

 

// cout << "test step: " << step << endl; sleep(1);

 

A1.waVe2(step, 210); // pass value for k and off-channel

 

if (A1.Delay == 0) {

// all channels on and some delay

A1.chArray = "11111111";

A1.Delay = 20000;

A1.waVe2(1, 0);

}

}

 

 

A1.Delay = 100000;

for (i=0; i<50; i++) {

if (i > 10) { A1.Delay = 30000; }

if (i > 30) { A1.Delay = 15000; }

A1.waVe();

}

 

A1.chArray = "10000000000001000100000100010000001000000000001000001000"; // bits for random on off

A1.Delay = 500000;

for (i=0; i<20; i++) {

if (i > 5) { A1.Delay = 100000; }

if (i > 10) { A1.Delay = 50000; }

A1.upload();

}

 

A1.Delay = 100000;

// Biuld_n_brake

A1.chArray = "100000000100000000100000000100001000100001000100001000100001000110001001010

00100100011000100111000101101000111001001110001011110001111010011110010111100011

11100111110101111100111111001111111011111101111111011111111111111111111110111111

01111111011111101111111001111110011111010111110011111100011110100111100101111000

11101000111001001110001011010001110010001100010010100010100100011000100001000100

01000100001000100001000000001000000001000000001";

for (i=0; i<20; i++) {

if (i > 10) { A1.Delay = 30000; }

A1.upload();

}

A1.Delay = 100;

A1.waVe2(10, 200);

 

 

// Brake_chase

A1.Delay = 300000;

A1.chArray = "100100100100100100100100";

for (i=0; i<30; i++) {

if (i > 10) { A1.Delay = 100000; }

A1.upload();

}

 

A1.Delay = 10000;

for (i=0; i<1000; i++) {

A1.zeroCross();

}

 

for (i=0; i<3; i++) {

A1.Delay = 700000;

for (j=0; j<20; j++) {

if (j > 3) { A1.Delay = 200000; }

if (j > 5) { A1.Delay = 150000; }

if (j > 15) { A1.Delay = 100000; }

A1.stepUpDown();

}

}

 

A1.Delay = 500;

A1.chArray = "1010101001010101"; // set chArray for tenies;

A1.waVe2(5, 200);

for (i=0; i<200; i++) {

A1.Delay = 500000;

if (i > 50) { A1.Delay = 200000; }

if (i > 100) { A1.Delay = 150000; }

if (i > 150) { A1.Delay = 100000; }

A1.upload();

}

 

for (i=0; i<10; i++) {

A1.nightRider();

}

 

for (i=0; i<2; i++) {

A1.dimShaker();

}

 

for (i=0; i<5; i++) {

A1.Delay = 500000;

A1.shooTingStar(3);

 

A1.Delay = 50000;

A1.shooTingStar(10);

 

A1.Delay = 10000;

A1.shooTingStar(5);

}

 

 

A1.Delay = 10000;

A1.MaxLevel = 100; // set the maximum brightness of the lights

// cout << "MaxLevel: " << A1.MaxLevel << endl;

A1.turnOn(5, 50);

A1.turnOn(2, 5);

A1.turnOn(10, 100);

A1.turnOn(1, 1);

 

A1.MaxLevel = 0;

// cout << "MaxLevel: " << A1.MaxLevel << endl;

A1.turnOn(5, 5);

A1.turnOn(5, 100);

A1.turnOn(2, 5);

A1.turnOn(1, 1);

 

A1.Delay = 100000;

A1.MaxLevel = 128;

// cout << "MaxLevel: " << A1.MaxLevel << endl;

A1.turnOn(5, 10);

A1.turnOn(5, 5);

A1.turnOn(2, 5);

A1.turnOn(10, 100);

 

A1.Delay = 100000;

for (i=0; i<5; i++) {

A1.ranDom(0); // latch all channels together

}

 

for (i=0; i<2; i++) {

A1.ranDom(1); // latch each channel one at a time

}

 

for (i=0; i<10; i++) {

A1.Delay = 10000;

A1.ranDom(0);

A1.Delay = 1000;

A1.shooTingStar(5);

}

 

A1.chArray = "1111111100000000"; // 50-50 Strobe

A1.Delay = 100000;

for (i=0; i<100; i++) {

if (i > 50) { A1.Delay = 50000; }

A1.upload();

}

 

A1.chArray = "110011000011001111100111111111110101010110101010000011111111000010000000010

00000010000000010000000010000000010000000010000000010000001000000100000010000001

00000100000010000001000000001111111101111111101111111101111111101111111101111111

01111111100001100000111100011001101100001110000001000011110000000011110000000000

00000111100000000110011000000000000110011000000001010101000000000010101010000000

10101010000000000011001100000000110011000000000000001111000000001111000000000000

00011110000000011110000";

for (i=0; i<2; i++) {

A1.Delay = 100;

A1.waVe2(10, 210);

A1.Delay = 1;

A1.waVe2(5, 180);

}

 

}

 

A1.turnOff();

}

Posted

thanks a lot guys. Haha, unfortunately I didn't check this until later and already submitted the assignment. I have a test in the morning for a Digital Circuits class so I didn't have much time to put into C++.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...