Program for this Alphabet pattern in c++ - c

Is there any other way to do this program with less number of loops.Its not much efficient
#include <iostream>
using namespace std;
int main() {
int i,j,n,s;
cout<<"Enter the value of n";
cin>>n;
for(i=1;i<=n;i++){
for(s=1;s<=n-i;s++){
cout<<" ";
}
char ch=97;
int k=1;
for(j=1;j<=(i*2-1);j++) {
if (k%2!= 0){
cout <<ch;
ch++;
} else {
cout<<" ";
}
k++;
}
cout<<endl;
}
}
output:
Enter the value of n6
a
a b
a b c
a b c d
a b c d e
a b c d e f

Try the following code-
#include<stdio.h>
main()
{
int i,j,k,num;
printf("Enter the number of letter \n");
scanf("%d",&num);
for(i=0;i<num;i++)
{
for(j=num-1;j>i;j--)
printf(" ");
for(k=0;k<=i;k++)
printf("%c ",(97+k));
printf("\n");
}
}
Sample input and output-
sathish#ubuntu:~/c/basics$ ./a.out
Enter the number of letter
4
a
a b
a b c
a b c d

#include <stdio.h>
int main(){
char *pat = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
int i, n;
printf("input n : ");
scanf("%d", &n);
for(i = 1;i<=n;++i)
printf("%*s%.*s\n", n - i, "", (i << 1) - 1, pat);
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
int n;
cout << "Enter the value of n : ";
cin >> n;
string spaces(n, ' ');
string pat("a b c d e f g h i j k l m n o p q r s t u v w x y z");
for(int i=1;i<=n;i++){
cout << spaces.substr(i);
cout << pat.substr(0, (i << 1) -1);
cout << endl;
}
}

Related

Why isn't my printf() statement in my searchPuzzle function isn't working?

For some reason, my print statement within my searchPuzzle function isn't working. Can you guys explain the reason why? I am trying to find certain words within a crossword puzzle which is 15x15. The word I am trying to find is the states with the US, like NewYork for example. The char ** arr represents the crossword puzzle. while the char** list represents the list of states. My go is for my function is to try to find the states in the crossword puzzle and to print out the states it does find. Int listsize has the value of 50. While n has the value of 15.
This is the cross word puzzle:
W D B M J Q D B C J N Q P T I
I R Z U X U Z E A O I O R T N
M N Z P L R N H L Y L X H M D
M Y E K A I D P I U L Y O W I
A O A B A R K U F V I H L A A
L O N M R X K I O J N A V R N
A E P T A A R A R T O W A I A
S U C Z A U S I N A I A L Z V
K O T A O N R K I S S I A O N
A H X S V K A I A E A I B N E
U D S X N X C C D W G S A A V
O I S D W L E J N J T X M H A
M O X W T N H Q D X O Q A Q D
R U U V G E O R G I A Q V D A
V F L O R I D A L G L W O X N
This is the list of states:
Alabama
Alaska
Arizona
Arkansas
California
Colorado
Connecticut
Delaware
Florida
Georgia
Hawaii
Idaho
Illinois
Indiana
Iowa
Kansas
Kentucky
Louisiana
Maine
Maryland
Massachusetts
Michigan
Minnesota
Mississippi
Missouri
Montana
Nebraska
Nevada
NewHampshire
NewJersey
NewMexico
NewYork
NorthCarolina
NorthDakota
Ohio
Oklahoma
Oregon
Pennsylvania
RhodeIsland
SouthCarolina
SouthDakota
Tennessee
Texas
Utah
Vermont
Virginia
Washington
WestVirginia
Wisconsin
Wyoming
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// DO NOT INCLUDE OTHER LIBRARY!
// Declarations of the two functions you will implement
// Feel free to declare any helper functions
void printPuzzle(char** arr, int n);
void searchPuzzle(char** arr, int n, char** list, int listSize);
// Main function, DO NOT MODIFY!!!
int main(int argc, char **argv) {
int bSize = 15;
if (argc != 2) {
fprintf(stderr, "Usage: %s <puzzle file name>\n", argv[0]);
return 2;
}
int i, j;
FILE *fptr;
char **block = (char**)malloc(bSize * sizeof(char*));
char **words = (char**)malloc(50 * sizeof(char*));
// Open file for reading puzzle
fptr = fopen(argv[1], "r");
if (fptr == NULL) {
printf("Cannot Open Puzzle File!\n");
return 0;
}
// Read puzzle block into 2D arrays
for(i=0; i<bSize; i++){
*(block+i) = (char*)malloc(bSize * sizeof(char));
fscanf(fptr, "%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c\n", *(block+i), *(block+i)+1, *(block+i)+2, *(block+i)+3, *(block+i)+4, *(block+i)+5, *(block+i)+6, *(block+i)+7, *(block+i)+8, *(block+i)+9, *(block+i)+10, *(block+i)+11, *(block+i)+12, *(block+i)+13, *(block+i)+14 );
}
fclose(fptr);
// Open file for reading word list
fptr = fopen("states.txt", "r");
if (fptr == NULL) {
printf("Cannot Open Words File!\n");
return 0;
}
// Save words into arrays
for(i=0; i<50; i++){
*(words+i) = (char*)malloc(20 * sizeof(char));
fgets(*(words+i), 20, fptr);
}
// Remove newline characters from each word (except for the last word)
for(i=0; i<49; i++){
*(*(words+i) + strlen(*(words+i))-2) = '\0';
}
// Print out word list
printf("Printing list of words:\n");
for(i=0; i<50; i++){
printf("%s\n", *(words + i));
}
printf("\n");
// Print out original puzzle grid
printf("Printing puzzle before search:\n");
printPuzzle(block, bSize);
printf("\n");
// Call searchPuzzle to find all words in the puzzle
searchPuzzle(block, bSize, words, 50);
printf("\n");
// Print out final puzzle grid with found words in lower case
printf("Printing puzzle after search:\n");
printPuzzle(block, bSize);
printf("\n");
return 0;
}
void printPuzzle(char** arr, int n){
// This function will print out the complete puzzle grid (arr). It must produce the output in the SAME format as the samples in the instructions.
// Your implementation here
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
printf("%c ", *(*(arr + i) + j));
}
printf("\n");
}
}
void searchPuzzle(char** arr, int n, char** list, int listSize){
// This function checks if arr contains words from list. If a word appears in arr, it will print out that word and then convert that word entry in arr into lower case.
// Your implementation here
for(int e = 0; e < listSize; e++){
for(int f = 0; f < strlen(*(list+e)); f++){
if(*(*(list + e) + f) >= 'a' && *(*(list + e) + f) <= 'z' ){
*(*(list + e) + f) = *(*(list + e) + f) - ('a' - 'A');
}
}
}
int k = 0;
for(int a = 0; a < listSize; a++){
for(int b = 0; b < n; b++){
for(int c = 0; c < n; c++){
if(*(*(list + a) + k) >= 'a' && *(*(list + a) + k) <= 'z' ){
*(*(list + a) + k) = *(*(list + a) + k) - ('a' - 'A');
}
if( *(*(list + a) + k) == *(*(arr + c) + b) ){
k++;
}
if( *(*(list + a) + k) != *(*(arr + c) + b) ){
k = 0;
break;
}
printf("%i ", k);
if ( k == (strlen(*(list+a))-1) ){
printf("Found: ");
for(int l = 0; l < strlen(*(list+a)); l++){
printf("%c", *(*(list + a) + l));
//printf("\n");
}
printf("\n");
k = 0;
break;
}
}
}
}
}
if( *(*(list + a) + k) == *(*(arr + c) + b) ){
k++;
}
if( *(*(list + a) + k) != *(*(arr + c) + b) ){
k = 0;
break;
}
In the first if, you increment k if you find a match. Then, in the next if, we are checking the next character in the word (since we did k++), with the same character *(*(arr + c) + b) from the crossword. For example, in NEWYORK, you match N with N (so far good), then compare E with N which is not equal, so it breaks out of loop. You should use an if .. else here, instead of two separate ifs, since the second condition should only be checked if the first is false.
When you find a non-matching character, you are using break. This will break out of the c loop, meaning that, if any character in a crossword line fails to match the word from list, the remaining characters in that line will not be checked. Here, you don't have to break out of the loop; setting k = 0 should be enough.
In *(*(arr + c) + b) you are incrementing c in the inner loop, so you are only checking for vertical matches in the crossword. If you want to check for horizontal matches also, you should also do the same checks after changing the nesting order of b and c loops. (OR you can check *(*(arr + b) + c)(changed b and c position) in the same loop and use another variable for horizontal in place of k. But note that this does not work if crossword is not square ie. not NxN)
As #bruno mentioned in comments, use arr[c][b] instead of *(*(arr + c) + b) as it is more readable and maintainable. Also use a loop to read characters in the crossword. That will be more maintainable than %c %c %c ...
In my linux machine, the following code is truncating one extra character from the words in list. Most likely because you are on Windows, and Windows uses \r\n line endings and Linux uses \n endings. So this won't work as you expect if your word list file was written in non-windows machine (Mac, Linux). If you want it to work anywhere, you can use strcspn function with \r\n to remove new line characters.
// Remove newline characters from each word (except for the last word)
for(i=0; i<49; i++){
*(*(words+i) + strlen(*(words+i))-2) = '\0';
}
After making changes, you should get this (tell if anything is missing):
Found:ALABAMA
Found:ALASKA
Found:ARIZONA
Found:CALIFORNIA
Found:FLORIDA
Found:GEORGIA
Found:HAWAII
Found:ILLINOIS
Found:INDIANA
Found:NEVADA
Since it is a school work, I will let you make the changes in code.
I modified the print function a bit. I suppose it should work:
#include <stdio.h>
#include <string.h>
#define ARRAY_SIZE(X) sizeof(X) / sizeof(X[0])
void printPuzzle(const char ** arrP, int size){
for (int i = 0; i < size; i++){
printf(*arrP++);
printf("\n");
}
}
// Main function, DO NOT MODIFY!!!
int main(int argc, char **argv) {
const char * puzzle [] = {"ABC","DEF","GHI"};
printPuzzle(puzzle, ARRAY_SIZE(puzzle));
return 0;
}

Stop printing repeated arithmetic expressions

I wrote this program in C to find all sums of three numbers that equal number n:
#include<stdio.h>
int main ()
{
int n;
printf("n:");
scanf("%d", &n);
int a,b,c;
a=0;
while(a<n)
{
a++;
b = 0;
while (b < n)
{
b++;
c = 1;
while (c < n)
{
if(a + b + c == n){
printf("%d + %d + %d = %d\n", a , b ,c ,n);
c++;
}
if (a + b + c != n){
c++;
}
}
}
}
}
What do I do to stop it from printing repeated arithmetic expressions, like 1 + 1 + 3 and 3 + 1 + 1, for example.
I would recommend something similar to what Ry-♦ suggested in the comments to your question.
Here is what it would look like in your code:
#include<stdio.h>
int main ()
{
int n;
printf("n:");
scanf("%d", &n);
int a,b,c;
a=0;
while(a<n)
{
a++;
b = a; /* start b at a to prevent duplicate sequences */
while (b < n)
{
b++;
c = b; /* start c at b to prevent duplicate sequences */
while (c < n)
{
if(a + b + c == n){
printf("%d + %d + %d = %d\n", a , b ,c ,n);
c++;
}
if (a + b + c != n){
c++;
}
}
}
}
}
Also in the comments of your question is a suggestion to use for loops. This would make your code a lot more compact and easier to read:
#include<stdio.h>
int main ()
{
int n;
printf("n:");
scanf("%d", &n);
int a,b,c;
for(a=0;a<n;a++)
for(b=a;b<n;b++)
for(c=b;c<n;c++)
if(a + b + c == n)
printf("%d + %d + %d = %d\n", a , b ,c ,n);
}

why is this code giving this output - "HCF is: 1" whenever a%b != 0?

This program is made to find the HCF of two integers a and b by the formula/algorithm - 'a = bq +r' where a and b are two numbers, q being the quotient and r is the remainder.
here is the code.
#include <stdio.h>
int main() {
int a, b;
printf("enter both numbers a>b to find HCF\n");
scanf("%d %d",&a, &b);
int q, r, hcf;
if(a%b == 0) {
r = 0;
hcf = r;
} else {
q = a/b;
r = a%b;
}
int i;
for(i = r; i = 0;) {
a = b;
b = i;
hcf = b;
q = a/b;
i = a%b;
}
printf("HCF is: %d", hcf);
return 0;
}
The for loop is not correct. You need to use == instead of =. The = operator is for assignment, == is for comparison. Also, you want the loop to stop when i == 0 so the condition should be i != 0. The following loop works for me:
for(i = r; i != 0; ) {
a = b;
b = i;
hcf = b;
i = a%b;
}

Inverted Triangle from String

I'm a bit confused on how to make an inverted triangle from user input so that the last character is removed each time and a space is added to the front of each row. So this is what I have now, it should be correct minus the spaces (which I can't seem to get to work for whatever reason). Should be a really simple for loop, but I just can't figure it out for the life of me.
Here's what it looks like when it's run now:
Enter a string: EXAMPLE
E X A M P L E
E X A M P L
E X A M P
E X A M
E X A
E X
E
and what I want it to look like:
Enter a string: EXAMPLE
E X A M P L E
E X A M P L
E X A M P
E X A M
E X A
E X
E
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main()
{
char string[100];
int c, k, length;
printf("Enter a string: ");
gets(string);
length = strlen(string);
printf("\n");
for(c=length; c>0; c--)
{
for(k=0; k<c; k++)
{
printf("%c ", string[k]);
}
printf("\n");
}
getch();
}
You just to add length - c spaces before printing each lines because the less characters you have to print, the more spaces you have to insert:
#include <stdio.h>
#include <string.h>
int main()
{
char string[100];
int c, k, length;
printf("Enter a string: ");
gets(string);
length = strlen(string);
printf("\n");
for(c=length; c>0; c--)
{
//Add some spaces
for(k=0; k < length - c ; k++)
{
printf(" ");
}
for(k=0; k<c; k++)
{
printf("%c ", string[k]);
}
printf("\n");
}
return 0;
}
Example with SAMPLE STRING:
Enter a string: SAMPLE STRING
S A M P L E S T R I N G
S A M P L E S T R I N
S A M P L E S T R I
S A M P L E S T R
S A M P L E S T
S A M P L E S
S A M P L E
S A M P L E
S A M P L
S A M P
S A M
S A
S
Just insert an increasing amount of spaces at every turn (i.e. printf space k times).

“Fun with Rotation” - Codechef September Long Challenge

Whatz wrong with that answer?Plz help me
question is:
You are given an array A of N integers. You are to fulfill M queries. Each query has one of the following three types:
C d : Rotate the array A clockwise by d units.
A d : Rotate the array A anticlockwise by d units.
R d : Query for the value of the element, currently being the d-th in the array A.
Input
The first line contains two numbers - N and M respectively.
The next line contains N space separated Integers, denoting the array A.
Each of the following M lines contains a query in the one of the forms described above.
Output
For each query of type R output the answer on a separate line.
Constraints
1 ≤ N ≤ 100000
1 ≤ M ≤ 100000
1 ≤ d ≤ N, in all the queries
1 ≤ elements of A ≤ 1000000
The array A and the queries of the type R are 1-based.
Example
Input:
5 5
5 4 3 3 9
R 1
C 4
R 5
A 3
R 2
Output:
5
3
3
Solution:
#include<stdio.h>
//#include<iostream>
//using namespace std;
int a[100001];
int index=0,n;
void clock(int);
void ant_clock(int);
void display(int);
int main()
{
unsigned int i,m;
char x;
int y;
scanf("%d %d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
while(m--)
{
scanf(" %c%d",&x,&y);
if(1 <= y <= n)
{
if(x=='R')
display(y);
else if(x=='A')
ant_clock(y);
else if(x=='C')
clock(y);
}
else
return 0;
}
return 0;
}
void display(int y)
{
int j=index,x=0;
while(1)
{
if(x==y-1)
break;
j++;
x++;
if(j==n)
j=0;
}
printf("%d",a[j]);
}
void ant_clock(int y)
{
int j;
for(j=0;j<y;j++)
{
index--;
if(index==-1)
index=n-1;
}
}
void clock(int y)
{
int j;
for(j=0;j<y;j++)
{
index++;
if(index==n)
index=0;
}
}
It seems to be a problem with the query indices being 1-based. And your code has unnecessary loops in it.
#include <stdio.h>
int arr[100000];
int main() {
int base = 0, size, i, m;
char x;
scanf("%d%d", &size, &m);
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
while (m-- > 0) {
scanf(" %c%d", &x, &i);
if (x == 'R') {
printf("%d\n", arr[(base + i - 1) % size]);
} else if (x == 'A') {
if ((base -= i) < 0)
base += size;
} else if (x == 'C') {
base = (base + i) % size;
}
}
return 0;
}

Resources