C++ :: Creating Diagonal Pattern By Given Function

Nov 5, 2013

How to create a diagonal pattern by the given function

void diagonal(int size, char op)

The function takes in 2 arguments, size and op and displays a diagonal line of op char. for example, diagonal (5,#) will produce the following output.

#
#
#
#
#

View 4 Replies


ADVERTISEMENT

C :: Recursive Function For Diagonal Winnercheck?

Oct 31, 2014

I am trying to learn so much C as possible by my own. I have learned a bit already and made my first game. I made a tictactoe with a 3x3 board that works great. But now i want to make it a NxN-board. But the problem right now is my checkwinner-function. I really don't know how I should check the diagonal for winner in the for loop. Earlier I have checked the diagonal manually like you can see down there.

Code:

for (Row = 0; Row < BOARDSIZE; Row++) {
if ((game[Row][0] == 'X' && game[Row][1] == 'X' && game[Row][2] == 'X') ||
(game[0][Row] == 'X' && game[1][Row] == 'X' && game[2][Row] == 'X') ||
(game[0][0] == 'X' && game[1][1] == 'X' && game[2][2] == 'X') ||
(game[0][2] == 'X' && game[1][1] == 'X' && game[2][0] == 'X'))
return 1;

I think I need two foor-loops in each other too and need to check the spots in the board like 0,0, 0+1,1, 0+2,2. Is that even possible?

View 3 Replies View Related

C++ :: Design Pattern For Function Operation

Dec 11, 2014

Any design pattern allows to describe operation like the following.

MyFunctionObject f;
//Init f...
MyFunctionObject g;
//Init g...
MyFunctionObject h = f(g) + g;

[Code] .....

I'm interested in design pattern which permits to model this kind of structure, if there's of course...

View 7 Replies View Related

C++ :: Design Pattern For Dynamic Function Parameter

Oct 26, 2014

I need to design an interface(a function prototype) that takes an argument which is used to pass information.
The information can be passed by independent modules and third party softwares and hence can vary today and in future.

Basically, the function interface(arg1, info)caters a niche service to many independent applications and needs to process based on requirements passed by applications in the argument(info, in example).

I am looking for a design pattern for the function parameter - info.

Should I use a void pointer that can be casted to respective application specific class in the function ? will this be a good C++ design ?

or should I take this parameter to be a pointer to a generic abstract class that points to the respective application specific specialization ?

Do we have some design pattern to address this so as to handle other unforeseen challenges ?

View 1 Replies View Related

C/C++ :: Design Pattern For Extensible Function Parameter

Oct 26, 2014

I need to design an interface(a function prototype) that takes an argument which is used to pass information.

The information can be passed by independent modules and third party softwares and hence can vary today and in future.

Basically, the function interface(arg1, info) caters a niche service to many independent applications and needs to process based on requirements passed by applications in the argument(info, in example).

I am looking for a design pattern for the function parameter - info.

Should I use a void pointer that can be casted to respective application specific class in the function ? will this be a good C++ design ?

or should I take this parameter to be a pointer to a generic abstract class that points to the respective application specific specialization ?

Do we have some design pattern to address this so as to handle other unforeseen challenges ?

View 3 Replies View Related

C++ :: Print Pattern Function That Handles Parameter As String

Sep 26, 2013

How to write a printPattern() function?

The function takes in a parameter i.e. a char pointer. It handles the parameter as a Cstyle string i.e. a NULL terminated char array. It does not make use of the stringclass or its associated functions. In other words, the function examines every char element in the array until it encounters the terminating NULL character.

Starting from this int main :

int main() {
char string1[] = "Application of C++";
printPattern(string1);
}

[Code].....

View 1 Replies View Related

C/C++ :: Recursive Function To Print Hour Glass Pattern

Oct 11, 2014

I need to make a recursive function(s) which prints the hour glass pattern. This is my code so far:

void HourGlass(int n) {
if (n==1){
cout<<"*"<<endl;
cout<<"*"<<endl;
} else {
stars(n-2);
HourGlass(n-2);

[Code] ....

Problem is, it doesn't print the spaces in front of the pattern. This is what it prints:

*****
***
*
*
***
*****

and this is what it is supposed to print:

*****
***
*
*
***
*****

Do I make a separate function for the spaces? How do I go about it?

View 7 Replies View Related

C++ :: Searching Char By Diagonal Not Only The Main

Mar 6, 2015

I have a problem with searching chars by diagonal not only the main, i have a chars in vector and I need to go though all possibilities (as shown in picture) the word has to be side/2 long so here i have 9, so word has to be 4 chars long how I need to do this?

View 2 Replies View Related

C# :: Printing All Values Above The Diagonal Line In 2D Array

Apr 11, 2014

I need to print all the values in a square 2d array that are above the diagonal line. So if I have this 6x6 array for instance:

{0,0,0,0,0,0},
{1,1,1,1,1,1},
{2,2,2,2,2,2},
{3,3,3,3,3,3},
{4,4,4,4,4,4},
{5,5,5,5,5,5}

I'd have to print the elements in italics. I've tried this:

for (i = 1; i < a.GetLength(0); i++) {
for (j = i-1; j >=0; j--)
Console.Write("{0,-2}", a[i, j]);
Console.WriteLine();
}

with the array mentioned above but for some reason it keeps printing the elements BELOW the line, when I need it to print those above it. I know that for arrays we usually start loops with a value of 0 but seeing as there should be nothing to print at [0,0] I started the loop at 1.

View 2 Replies View Related

C/C++ :: 2D Array For Printing Both Diagonal Elements Of Matrix

Jan 31, 2014

what if we want to print both the diagonal elements!!!!

View 1 Replies View Related

C++ :: Find Biggest Part Of Column Over Diagonal Of Matrix

Jan 23, 2013

How to find the biggest column of the matrix (higher of main diagonal) Here is the draft of code that i desighned/ Of cause it has a several mistake. My task is here to create the matrix, allocate the dynamic memory for it, and to find the biggest sum of the column elements that is located over main diagonal. So main points is correct memory allocation, and the sorting to the biggest sum of column higher than diagonal.

#include<iostream>
#include<conio.h>
#include<time.h>
using namespace std;

[Code]....

View 5 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 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 Custom Audio Function

Apr 2, 2013

I'm trying to make a windows-focused , I will make it portable after , audio function that plays sounds according to my midi file. I know there is playsound, but it's not what I desire. I'm curious if Beep plays through the sound card or is similar to printf("a") ? I'm just looking for a low level solution.

View 1 Replies View Related

C++ :: Creating A Function To Return A String

Aug 13, 2014

I need a function to return a string

I need to pass input as "a,b,c,a,c,d,e"

function should return out put as

"a,b,c,d,e"

View 2 Replies View Related

C++ :: Creating A Variable Inside Function Arguments

May 15, 2013

In the following code:

#include <iostream> // For stream I/O
using namespace std;
int function(int a) {
return a;
}
int main() {
function(int b);
}

Why is creating a variable inside the function argument list not allowed. Any reason other then for the language syntax or just for the language syntax?

View 19 Replies View Related

C++ :: Implementing Classes And Creating A Driver Function?

Nov 30, 2012

For this problem, you will design and implement 2 classes and then write a driver function to test these classes. The first will be a C++ class for an abstract data type color with a public enumeration type colorType that has the color values shown in Listing 10.8. Your abstract data type should have an attribute for storing a single value of type colortype and member functions for reading (readColor) and writing (writeColor) a color value as well as setting and accessing it. The function readColor should read a color as a string and store the corresponding color value in the value attribute of a type color object. The function writeColor should display as a string the value stored in the value attribute of a type color object (see Figure 7.5). Modify class circle and the driver function in Listing 10.9 to include and use this class. You'll need to remove the declaration for color in class circle. Test your modified driver function with the new color and circle classes.

The second class will be to design and implement a rectangle class similar to class circle. Be sure to incorporate the new color class you have written and tested in the first part of the programming exercise. Write a client program that asks the user to enter a shape name (circle or rectangle) and then asks the user for the necessary data for an object of that class. The program should create the object and display all its attributes.

The circle class .h and .cpp files as well as the original driver function will be supplied. You are to provide the .h and .cpp files for the new color class and the modified driver function as well as the .h and .cpp files for the rectangle class and the client program that uses all three classes.

What I have so far, and I'm pretty lost;

color.h

Code:

Code:
//color.h
//Color class definition
#include "stdafx.h"
#ifndef COLOR_H
#define COLOR_H
class color {
public:
enum colorType {black, blue, green, cyan, red, magenta, brown, lightgray, nocolor};

[code].....

View 4 Replies View Related

C++ :: Creating Function That Outputs Either 1 Or 0 Depending On Input After Checking

Feb 18, 2014

I am trying to simplify my code by creating a function which takes an input then checks whether it is "y" or "n" then outputs either 1 or 0 depending on the input. So far this is what I have

int Choice(string choice) {
while(choice.compare("n") != 0 && choice.compare("N") != 0 && choice.compare("y") != 0 && choice.compare("Y") != 0){
cout << "Please enter a valid input either [y/n] : " << endl;
cin.clear(); cin.ignore(); cin >> choice;

[Code] ...

And I call it in the program using

cout << "Do you wish to change the hubble type of any galaxies? [y/n]" << endl;
cin >> choice;
while(Choice(choice) == 1){
....
cout << "Do you wish to change the hubble type of another galaxy? [y/n]" << endl;
cin << choice;
}

It compiles fine but displays some bizarre behaviour, I need to end the line twice in order for the program to continue and sometimes it just stops working (doesn't exit just appears to be stuck in a loop).

View 2 Replies View Related

C++ :: Creating Recursive Function That Will Print Out A Number To A Power

Apr 9, 2013

I am trying to create a recursive function that i can call on in order to take a user inputed base and exponent and give final answer this is what i have but im completely lost after this i dont even know how to continue. What i have so far

#include <iostream>
using namespace std;
int Exp(int x,int y){
if(base <= 1 || exp == 0) return 1;
if(exp == 1) return base;
int main(){
int number, exp;

[Code] .....

After i set the base situations im not sure how to get the function to make the function take the base to the exponent recursively.

View 3 Replies View Related

C++ :: Creating Stand Alone Function That Works With Member Functions?

Nov 1, 2014

I was able to get this program running. Now I working on taking one of my member functions and turning it into a standalone function. I choose the create_board() function. Yet, if I declare it in my header file or my main.cpp it cant access any info from the original member functions?

main.cpp
#include "ticTacToe.h"
#include <iostream>
#include <limits>

[Code]....

View 2 Replies View Related

Visual C++ :: Implementing Classes And Creating Driver Function?

Nov 30, 2012

I'm am having a few issues with this program, they stem from passing the color.h and .cpp into another .h and .cpp, and frankly getting how those two things really fit together.

For this problem, you will design and implement 2 classes and then write a driver function to test these classes. The first will be a C++ class for an abstract data type color with a public enumeration type colorType that has the color values shown in Listing 10.8. Your abstract data type should have an attribute for storing a single value of type colortype and member functions for reading (readColor) and writing (writeColor) a color value as well as setting and accessing it. The function readColor should read a color as a string and store the corresponding color value in the value attribute of a type color object. The function writeColor should display as a string the value stored in the value attribute of a type color object (see Figure 7.5). Modify class circle and the driver function in Listing 10.9 to include and use this class. You'll need to remove the declaration for color in class circle. Test your modified driver function with the new color and circle classes.

The second class will be to design and implement a rectangle class similar to class circle. Be sure to incorporate the new color class you have written and tested in the first part of the programming exercise. Write a client program that asks the user to enter a shape name (circle or rectangle) and then asks the user for the necessary data for an object of that class. The program should create the object and display all its attributes.

The circle class .h and .cpp files as well as the original driver function will be supplied. You are to provide the .h and .cpp files for the new color class and the modified driver function as well as the .h and .cpp files for the rectangle class and the client program that uses all three classes.

color.h

Code:

//color.h
//Color class definition
#include "stdafx.h"
#ifndef COLOR_H

[Code].....

View 3 Replies View Related

Visual C++ :: Creating A Function That Needs To Determine Perfect Scores

Jun 24, 2013

This program is incomplete as I am having difficulty creating the function that needs to find the number of perfect scores entered by the user. I have everything but the function complete ,here is my code:

Code:
// Write a modular program that accepts at least 10 integer test scores from the user and stores them in an array.
// The main should display how many perfect scores were entered (i.e., scores of 100), using a value-returning countPerfect function.
// Input validation: Do not accept scores less than 0 or greater than 100.

#include <iostream>
using namespace std;
int countPerfect(int intArray[], int); // Function prototype

[Code] ....

View 14 Replies View Related

C++ :: Recursive Function - Creating Vector Of Every Unique Combination Of Commands

Mar 24, 2013

I'm trying to write a recursive function that takes in a vector of strings that contains

"1 forward", "2 forward", "rotate left", "2 backwards" ... etc

how can I write recursive function that creates a vector of every unique combination of commands?

View 8 Replies View Related

C++ :: Creating User Defined Function To Count Number Of Vowels

Oct 7, 2013

The output I'm getting here just counts every letter in the sentence and counts them as vowels. I'm trying to make the user defined function return the amount of vowels within the sentence.

#include <iostream>
#include <iomanip>
#include <cmath>

[Code].....

View 4 Replies View Related

Visual C++ :: Dice Game - Creating Pass By Value Function Reference

Nov 27, 2014

i have a project where i create a dice game, the user rolls 2 dice and the computer roles 2 dice. the player that wins 3 out of 5 rolls wins the game. I have completed the requirements, but i wanted to create a pass by value function for "void Dice()", I'm not too sure how that works?

Code:

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
//creating my variables and their values
int compinput;

[Code] .....

View 5 Replies View Related

Visual C++ :: Creating Program To Make Sine Function Without Any Library Other Than Iostream?

Nov 10, 2012

My assignment is to create a C++ Program to find the sine of a number without any library other than iostream by using Taylor Series:

sin (x) = (x/1!) - (x^3/3!) + (x^5/5!) - (x^7/7!) + (x^9/9!) ...... (x^n/n!).

Here is what i have done till now:

#include <iostream>
double fact (int f); //declaration of factorial function
double power(double x, int y); //declaration of power function
double sin(int x); //declaration of sine function
//double cos(int x); //declaration of cosine function
//double tan(int x); //declaration of tangent function

[code]....

View 3 Replies View Related







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