I need help on writing functions with c++ on CodeBlocks theres a good amount so
ID: 3662628 • Letter: I
Question
I need help on writing functions with c++ on CodeBlocks theres a good amount so please can you help me with all of them
1)Prompt the user to enter a single word. If the word they enter is
longer than six letters display "long word", otherwise display
"short word". Two sample interactions follow:
* Enter a word: monkey
* short word
* Enter a word: monkeys
* long word
2)* Prompt the user to enter a line of text, read it and print it back
out as a quotation. Sample interaction:
* Enter a line of text: Every good boy does fine.
* You entered "Every good boy does fine."
3)*Read a single word from the user and print the letters of that
word in reverse order. Sample interaction:
* Enter a word: hello
* Your word backwords is: olleh
4)* Read a line of text from the user and print the line in all upper
case (non-alphabetic characters should just be printed as-it).
Sample interaction:
* Enter a line of text: Hello fred!
* Screaming version of your text: HELLO FRED!
5)* Write a while loop that displays the integers from -5 to 17
(inclusive of both), one value per line.
6)* Write a for loop that displays the integers from 1 to 7
(inclusive of both), one value per line.
7)* Write a while loop that displays the integers, counting by eights,
starting at 3 and never exceeding 100.
8)* Write a for loop that displays the integers from 18 down to 0
(inclusive of both).
9)* Write a for loop that displays all of the even integers between 1
to 76 (inclusive of both).
10)* Write a while loop that displays all of the odd integers between
-17 and 3 (inclusive of both ends).
11)* Write a while loop that, for each integer from 1 to 40, displays
the integer, a colon, a space and the word "odd" if that integer
is odd or the word "even" otherwise. Your output should
resemble:
* 1: odd
* 2: even
* 3: odd
* ...
12)* Read an odd non-negative integer from the user. If their input is
invalid (even or negative), display an error message and read a
new value. Repeat until they input a valid value. A sample
* interaction:
* Enter an odd non-negative integer: -3
* That value is invalid, try again.
* Enter an odd non-negative integer: 8
* That value is invalid, try again.
* Enter an odd non-negative integer: 12
* Thank you!
13)* Read an integer, call it side, between 3 and 70 from the user
(display an error and repeat until they enter an integer between
3 and 70). Display a box made of X's with the whose width and
height are both side (a square, although it won't look very
square-ish on your screen). For example, if the user enters 5,
your output would be:
* XXXXX
* X X
* X X
* X X
* XXXXX
14)* Read an odd integer between 1 and 71 from the user (display an
* error and repeat until they enter an odd integer between 1 and
* 71). Display a diamond made of X's with the corresponding width.
* For example, if the user enters 5 your output would be:
* X
* XXX
* XXXXX
* XXX
* X
15)* Create an array of int's with the values: 9, -1, 32, 18, 54, 77, and 1.
Display the ints in this array, one per line.
Explanation / Answer
//1.C++ prompt for a word then print short word
//if size of otherwise print long word
#include<iostream>
#include<string>
using namespace std;
int main()
{
//declare a character array of size,40
char word[40];
cout<<"Enter a word :";
cin>>word;
//call strlen function word
int lenght=strlen(word);
//Check if lenght is 6
if(lenght==6)
cout<<"short word"<<endl;
else
cout<<"long word"<<endl;
system("pause");
return 0;
}
---------------------------------------------------------------------
//2.C++ prompt for a sentence then print sentence
//to console
#include<iostream>
#include<string>
using namespace std;
int main()
{
//declare a character array of size,40
char sentence[80];
cout<<"Enter a line of text :";
//call gets method to read sentence
gets(sentence);
cout<<"You entered ""<<sentence<<"""<<endl;
system("pause");
return 0;
}
Enter a line of text :Every good boy does
You entered "Every good boy does"
-----------------------------------------------------------------------
//3.C++ prompt for a word then print word in reverse order
//to console
#include<iostream>
#include<string>
using namespace std;
int main()
{
//declare a character array of size,40
char word[80];
cout<<"Enter a word :";
cin>>word;
int len=strlen(word);
cout<<"Your word backwrods is "<<strrev(word)<<endl;
system("pause");
return 0;
}
Enter a word :hello
Your word backwrods is olleh
-------------------------------------------------------------------------------
//4.C++ prompt for a word then prints letters in upper case
#include<iostream>
#include<string>
using namespace std;
int main()
{
//declare a character array of size,40
char word[80];
cout<<"Enter a word :";
gets(word);
int len=strlen(word);
cout<<"Screaming version of your text : ";
for(int index=0;index<len;index++)
{
if(word[index]>='a' || word[index]<='z')
{
char upper=toupper(word[index]);
cout<<upper;
}
}
system("pause");
return 0;
}
Enter a word :Hello fred!
Screaming version of your text : HELLO FRED!
-------------------------------------------------------------------------------------
//5.C++ program that prints -5 to 17 using while loop
#include<iostream>
using namespace std;
int main()
{
int index=-5;
while(index<=17)
{
//print one number per line
cout<<index<<endl;
index++;
}
system("pause");
return 0;
}
output:
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-----------------------------------------------------------------------------
//6.
//C++ program that prints 1 to 7 using for loop
#include<iostream>
using namespace std;
int main()
{
for(int index=1;index<=7;index++)
{
//print one number per line
cout<<index<<endl;
}
system("pause");
return 0;
}
output:
1
2
3
4
5
6
---------------------------------------------------------------------------------
//7.
//C++ program that prints numbres starting from 3 and couting by 8 using
//for loop
#include<iostream>
using namespace std;
int main()
{
for(int index=3;index<100;index=index+8)
{
cout<<index<<endl;
}
system("pause");
return 0;
}
output:
3
11
19
27
35
43
51
59
67
75
83
91
99
---------------------------------------------------------------------------------------------------
//8.
//C++ program that prints numbres starting from 18 upto 0
//for loop
#include<iostream>
using namespace std;
int main()
{
for(int index=18;index>=0;index--)
{
cout<<index<<endl;
}
system("pause");
return 0;
}
output:
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
---------------------------------------------------------------------------------------
//9.
//C++ program that prints numbres starting from 1 upto 76(inclusive)
//for loop
#include<iostream>
using namespace std;
int main()
{
for(int index=1;index<=76;index++)
{
cout<<index<<endl;
}
system("pause");
return 0;
}
---------------------------------------------------------------------------------
//10.
//C++ program that prints odd integer numbres starting from -17 upto 3(inclusive)
//for loop
#include<iostream>
using namespace std;
int main()
{
for(int index=-17;index<=3;index=index+2)
{
cout<<index<<endl;
}
system("pause");
return 0;
}
output:
-17
-15
-13
-11
-9
-7
-5
-3
-1
1
3
----------------------------------------------------------------------------------------------------------------
//11
//C++ program prints 1 to 40 numbers with even or odd
#include<iostream>
using namespace std;
int main()
{
for(int index=1;index<=40;index++)
{
if(index%2==0)
cout<<index<<": even" <<endl;
else
cout<<index<<": odd" <<endl;
}
system("pause");
return 0;
}
1: odd
2: even
3: odd
4: even
5: odd
6: even
7: odd
8: even
9: odd
10: even
11: odd
12: even
13: odd
14: even
15: odd
16: even
17: odd
18: even
19: odd
20: even
21: odd
22: even
23: odd
24: even
25: odd
26: even
27: odd
28: even
29: odd
30: even
31: odd
32: even
33: odd
34: even
35: odd
36: even
37: odd
38: even
39: odd
40: even
---------------------------------------------------------------------------------------------
//12.
//C++ program display an error message and read a
//new value. Repeat until they input a valid value.
#include<iostream>
using namespace std;
int main()
{
bool repeat=true;
int nonnegativeodd;
while(repeat)
{
cout<<"Enter an odd non-negative integer: ";
cin>>nonnegativeodd;
if(nonnegativeodd<0 ||nonnegativeodd%2==0)
cout<<"That value is invalid, try again."<<endl;
else
repeat=false;
}
system("pause");
return 0;
}
Enter an odd non-negative integer: -3
That value is invalid, try again.
Enter an odd non-negative integer: 8
That value is invalid, try again.
Enter an odd non-negative integer: 12
That value is invalid, try again.
Enter an odd non-negative integer: 5
------------------------------------------------------------------------------------------------
//13.
//C++ program prompt for a value in a range of 3 and 70(inclusive)
//and print the squre
#include<iostream>
using namespace std;
int main()
{
int value;
do
{
cout<<"Enter a value to print a square: ";
cin>>value;
if(value<3 ||value>70)
cout<<"That value is invalid, try again."<<endl;
}while(value<3 ||value>70);
for(int index=1;index<=value;index++)
{
cout<<"X";
}
cout<<endl;
for(int index=1;index<value-1;index++)
{
cout<<"X";
for(int space=1;space<=value-2;space++)
cout<<" ";
cout<<"X";
cout<<endl;
}
for(int index=1;index<=value;index++)
{
cout<<"X";
}
cout<<endl;
system("pause");
return 0;
}
output:
Enter a value to print a square: 2
That value is invalid, try again.
Enter a value to print a square: 1
That value is invalid, try again.
Enter a value to print a square: 71
That value is invalid, try again.
Enter a value to print a square: 5
XXXXX
X X
X X
X X
XXXXX
------------------------------------------------------------------------------------------------------------
14:
//C++ program prompt for a value in a range of 3 and 71(inclusive)
//and print the pattern
#include<iostream>
using namespace std;
int main()
{
int value;
do
{
cout<<"Enter a value to print a square: ";
cin>>value;
if(value<3 ||value>71)
cout<<"That value is invalid, try again."<<endl;
}while(value<3 ||value>71);
int space=value/2;
for(int index=1;index<=value;index=index+2)
{
for(int i=1;i<=space;i++)
cout<<" ";
for(int i=1;i<=index;i++)
cout<<"X";
space--;
cout<<endl;
}
space=value/2;
for(int index=value/2+1;index>=1;index=index-2)
{
for(int i=space;i>=1;i--)
cout<<" ";
for(int i=1;i<=index;i++)
cout<<"X";
space--;
cout<<endl;
}
cout<<endl;
system("pause");
return 0;
}
Output:
Enter a value to print a square: 5
X
XXX
XXXXX
XXX
X
---------------------------------------------------------------------------------
//15.
//C++ program prints an array elements
#include<iostream>
using namespace std;
int main()
{
//declare an array of size ,7
const int size=7;
int arr[size]={9, -1, 32, 18, 54, 77,1};
for(int index=0;index<7;index++)
cout<<arr[index]<<endl;
cout<<endl;
system("pause");
return 0;
}
output:
9
-1
32
18
54
77
1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.