473,789 Members | 2,931 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a program using four arrays of pointers

91 New Member
hi, i'm writing a program that is using 4 arrays of pointers to char,called article,noun,ve rb and preposition.the program should create a sentence by selecting a word at random from each array in the following order:article,n oun,verb preposition,,ar ticle and noun.as each word is picked,it should be concatenated to the previous words in an array that is large enough to hold the entire sentence. the words should be separated by spaces.when the final sentence is output,it should start with a capital letter and end with a period.the program should generate 20 such sentences.

what i have deduced from this statement is that ill be using 4 arrays of pointers.so may you help me to have an idea of where to start because i don't have an idea.thanks
Sep 18 '07
16 5835
Nkhosinathie
91 New Member
That was a short outline of the steps you have to implement in order to produce
a (pseudo) random element from a list "w0,w1,w2,w 3 ...,wn". Read it again.

kind regards,

Jos
please check the following code.
Expand|Select|Wrap|Line Numbers
  1. // program uses randon number generation to create sentences
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include<ctype.h>
  6. #include<cstring>
  7. const int row= 20;
  8.  
  9. const int column= 80;
  10. void touppercase(char *string);
  11. void concatenate(char *array[][column],int element,char *string[],int size);
  12.  
  13. using std::cout;
  14. using std::endl;
  15.  
  16. int main()
  17. {
  18.  
  19. srand(time(NULL));
  20.  
  21. char *array[row][column] = {" "};
  22. int position= 0;
  23. int counter= 1;
  24. char *article[5]= {"the","a","one","some","any"};
  25. char *noun[5]= {"boy","girl","dog","town","car"};
  26. char *verb[5]= {"drove","jumped","ran","walked","skipped"};
  27. char *preposition[5]= {"to","from","over","under","on"};
  28.  
  29. do
  30. {
  31.  
  32.  
  33. if(counter == 20)
  34. touppercase(article[position]);
  35.  
  36. concatenate(array,counter,article,row);
  37.  
  38. concatenate(array,counter,noun,row);
  39.  
  40. concatenate(array,counter,verb,row);
  41.  
  42.  
  43. concatenate(array,counter,preposition,row);
  44.  
  45.  
  46. }while(counter < 21);
  47. for(int i =0;i<20;i++)
  48. for(int j =0;j<20;j++)
  49. cout<<array[i][j]<<endl;
  50.  
  51. return 0;
  52. }
  53.  
  54. void touppercase(char *string)
  55. {
  56. int i =0;
  57. while((*string != '\0') && (i != 1))
  58. {
  59. *string = toupper(*string);
  60. ++i;
  61. }
  62. }
  63. void concatenate(char *array[][column],int element, char *string[],int size)
  64. {
  65. int position = 0;
  66. position = rand() % 5;
  67. array[element][column] = string[position]; 
  68.  
  69. }
Sep 19 '07 #11
JosAH
11,448 Recognized Expert MVP
So now you've changed the implementation from this:

Expand|Select|Wrap|Line Numbers
  1. char arrayNoun[50]="boy,girl,dog,town,car";
  2.  
to this:

Expand|Select|Wrap|Line Numbers
  1. char *noun[5]= {"boy","girl","dog","town","car"};
  2.  
That renders my remarks, based on your previous implementation, completely useless.

kind regards,

Jos
Sep 19 '07 #12
pbmods
5,821 Recognized Expert Expert
Heya, Nkhosinathie.

What is your code doing that you don't want it to do? Give an example.
What is your code *not* doing that it is supposed to? Give an example.
Sep 19 '07 #13
sicarie
4,677 Recognized Expert Moderator Specialist
Nkhosinathie-

Please do not double-post - try to keep all your questions on the same topic in one thread.

Also, you are a member, so you should be using code tags around your code. They are [CODE=cpp] and [/code].

Thanks
Sep 19 '07 #14
Nkhosinathie
91 New Member
Heya, Nkhosinathie.

What is your code doing that you don't want it to do? Give an example.
What is your code *not* doing that it is supposed to? Give an example.
the program doesn't want to print anything. when i compile it,it shows just a space
thanks.
Sep 20 '07 #15
Nkhosinathie
91 New Member
Nkhosinathie-

Please do not double-post - try to keep all your questions on the same topic in one thread.

Also, you are a member, so you should be using code tags around your code. They are
Expand|Select|Wrap|Line Numbers
  1.  and 
.

Thanks
i apologise for that,and i'm still not sure how to wrap my code to code tags.
Sep 20 '07 #16
sicarie
4,677 Recognized Expert Moderator Specialist
i apologise for that,and i'm still not sure how to wrap my code to code tags.
If you look at my post above, you put the code in the space of the 'and' in between the two tags. Then if you look at your reply, you will see how the code is translated.
Sep 20 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2257
by: Brian McGuinness | last post by:
I have a question about using the STL transform algorithm in a function. What I want to do is define a group of array classes to represent APL-style arrays (arrays in which the number of dimensions and the length of any dimension can be changed at any time). What I currently plan is to have an abstract base class at the top, to allow polymorphism, e.g.: #include "basedefs.h" // Basic data types, e.g. typedef long Integer
12
10091
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display it. here is the 4 errors. c:\C++\Ch15\Employee.h(29): error C2440: '=' : cannot convert from 'char ' to 'char '
26
2269
by: mwt | last post by:
Hello. Today I wrote my first program in C. It adds up the elements in an array. I am just beginning to learn this language. Any tips or pointers about better ways to write/structure/format/etc. this code would be much appreciated. Thanks. mwt. #include <stdio.h> int add_array(int arr, int arr_size) {
17
3265
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
34
29904
by: Tom | last post by:
I'd greatly appreciate advice and code snippets on how to create a ram disk within a C/C++ program. I also need to be able to determine the free space. Thanks in advance for any help.
23
2270
by: mike3 | last post by:
Hi. I seem to have made some progress on finding that bug in my program. I deactivated everything in the bignum package that was used except for the returning of BigFloat objects. I even crippled all the constructors. So now all the operations and constructors that were used do is just return BigFloats but no memory is actually accessed at any point, nor is any allocated. However, when I reenable those parts of the constructor that...
334
11615
by: Antoninus Twink | last post by:
The function below is from Richard HeathField's fgetline program. For some reason, it makes three passes through the string (a strlen(), a strcpy() then another pass to change dots) when two would clearly be sufficient. This could lead to unnecessarily bad performance on very long strings. It is also written in a hard-to-read and clunky style. char *dot_to_underscore(const char *s) { char *t = malloc(strlen(s) + 1); if(t != NULL)
6
1668
by: jason | last post by:
Hello, I have a question about what kind of datastructure to use. I'm reading collumn based data in the form of: 10\t12\t9\t11\n 24\t11\t4\t10\n ..... I now have a structure which allows me to access the data like this: x->row.coll.value.d;
4
11656
by: wutang | last post by:
Create a program that displays the sum of the sales amounts made in each of four regions (North, South, East, West) during a three month period. The program should display the total sales made during the three months. 1. Complete the program by entering the C++ code that allows the user to enter four sets(one set for each region) of three sales amounts (one sales amount for each month). The program should display each region's total sales for...
0
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9984
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.