Nested if else if statements with multiple variable for loops - vb.net

Below is the code I am using for a program which is to be used for an airplane seating selection. It seats passengers based upon either a smoking or non-smoking preference.
I am using nested if else statements with 2 variables in each as counters and accumulators as well as nested if-then-else statements which should fill the assign[] array of structures.
Am I unable to copy arrays of structures directly into other arrays of structures using counters for the indexes? Help*/
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace System;
using namespace std;
struct passengers{
char plname[15] ;//passenger name in array
char pfname[15];
char stPref;// = seat preference, either S smoking or N non-smoking
int row;//row 0-40 (41 rows) index data, to be used later to match the index to the structure of array index
int col; //column 1-3
};struct passengers waiting[41];//Hard Coded Array of struct passengers waiting members for use with output testing
//= {{"WOODS","BILL",'S',10,2},{"BORAX","M T", 'S', 9, 2},{"THOMAS","LINDA",'N',7,2},{"HAYWARD","MARK",'N',7,3},{"SWIFT","TOM",'N',8,1},{"DOBBS","DR.",'N',8,2},{"GOLDEN","VINNIE",'N',8,3},{"HACKER","E",'N',5,3},{"RUSSEL","STEVE",'N',5,1},{"CHAMPION","SPARKY",'N',3,1},{"INNERMENTS","TEX",'S',8,2}};
//Hard Coded Array of struct passengers all[41]. This is a list of all people that want on the plane
struct passengers all[41] = {{"SMITH","JOHN",'N',3,2},{"STEVE","JOBS",'S',9,3},{"CAPONE","AL",'N',3,2},{"JACKSON","MICHAEL",'S',8,1},{"HUMPHREY","H",'S',8,2},{"RINEHART","JIM",'N',3,1},{"EASTMAN","KEN",'N',1,1},{"WINSTON","SALEM",'S',8,3},{"SMYTHE","SUSAN",'S',9,1},{"KENDRIKS","AL",'N',9,2},{"ALLISON","DENNIS",'S',9,3},{"GREENBLATT","RICHARD",'S',10,1},{"DAVIS","BOB",'S',10,3},{"WOODS","BILL",'S',10,2},{"BORAX","M T",'S',9,2},{"HENRY","JOHN",'N',1,2},{"STEVASON","E",'N',1,3},{"WILLIAMS","KEN",'N',2,1},{"SMITH","MARTHA",'N',2,2},{"KOCH","JOE",'N',2,3},{"PACKARD","H P",'N',3,3},{"RINEHART","JANE",'N',3,2},{"SWENSON","CECIL",'N',4,1},{"CORY","TROY",'N',4,2},{"BYES","NICKOLINE",'N',4,3},{"BYES","JENNIFER",'N',5,3},{"HARRIS","JOHN",'N',5,2},{"HARRIS","JUDY",'N',5,1},{"HARTMAN","F G",'N',6,1},{"SOUSE","D T",'N',6,2},{"JOHNSON","MAGIC",'N',6,3},{"LAMPTON","GEORGE",'N',7,1},{"THOMAS","LINDA",'N',7,2},{"HAYWARD","MARK",'N',7,3},{"SWIFT","TOM",'N',8,1},{"DOBBS","DR.",'N',8,2},{"GOLDEN","VINNIE",'N',8,3},{"HACKER","E.",'N',5,3},{"RUSSEL","STEVE.",'N',5,2},{"CHAMPION","SPARKY.",'N',3,1},{"INNERMENTS","TEX.",'S',8,2}};
struct passengers assign[30];//Array of structures for the actual seating assignment
//Initializes char array plane so all seats are empty
char plane[10][3] = { {'E','E','E'},{'E','E','E'},{'E','E','E'},{'E','E','E'},{'E','E','E'},{'E','E','E'},{'E','E','E'},{'E','E','E'},{'E','E','E'},{'E','E','E'} };
int x,y,z,a,b,c,count = 0;//Declares integer variables for use with accumulators and counters.
void entList();//Enter function prototype
void prtList();//Print function prototype
void seatPrgm();//Enters Passengers based on seat availability
/******************************************* Main Program ********************************************************************/
int main()
{
/********** Decision, Selection, Iteration Section... Assigns Seats based on Availability **********/
while (z<41)
{
//seatPrgm();
//************************************ User Case Selection Menu *******************************************/
char menu = '#';//Declares menu a character variable.This variable will be used for the menu selections and has a default value
//of # to ensure no problems with it's buffer.
printf("\t\t\tWelcome to Fast Flights.\n\n\t\tPlease Make a selection from thew menu\n\n");//User Prompt
printf("\t\tE to Enter Passenger Seating Information\n\t\tP to Print the Assigned Seating Report\n\t\tQ to quit.\n\n");
scanf("%c",&menu);//scans in the menu selection
while(menu !='Q')//While variable menu does not equal Q...
{
menu =(toupper(menu));
switch(menu)//Switch Statement generates a case selection menu...
{
case 'E' : entList();
printf("\n\t\t\n\n");
system("pause");
scanf("%c",&menu);
break;
case 'P' : prtList();
printf("\n\t\t\n\n");
system("pause");
scanf("%c",&menu);
break;
default : if (menu!= 'Q')
printf("\n\t\nPlease enter one of the selections\n");
else printf("\n");
scanf("%c",&menu);
system("pause");
break;
}
}
system("pause");//namespace system pause Pauses the program long enough to display data
return 0;//returns null value to int main()
}
}
void entList()
{ /*count=0;
printf("\tPlease enter passenger name and seating preferences\n");
printf("\nPassenger Name:");
gets(assign[count].pname);
// scanf("%c"); // this gets the enter key left over in the buffer - do not need this after a gets
printf("\nSeating Preference Smoking or Non-Smoking? Please enter S or N:");
scanf("%c",&assign[count].stPref);
// it was skipping because I had formatting in the scanf statement
printf("\nEnter the Seat Row Number:");
scanf("%c",&assign[count].row);
printf("\nEnter the Seat Column Number:");
scanf("%c",&assign[count].col);
count++;*/
}
//Print Function Header
void prtList()
{
/************** This section prints the first header for the Non-Smoking Section ***********************/
printf("\n\n");
for (x = 0; x<80; x++)
printf("*"); //Prints the asterisks in the heading
printf("\n");
for (x = 0; x<6; x++)
printf("*");
printf("\n");
printf("*\t\t\tNon Smoking Section\n");
printf("*\n");
for (x = 0; x<80; x++)
printf("*");
for (x = 0; x<6; x++)
printf("*");
printf("*\n");
/****************** Prints the Non-Smoking Section Seating Assignments ****************************************/
printf("\n");
x=0;
for(a=0;a<7;a++)
{
for(b=0;b<3;b++) //Prints passengers assign.[30] structure of arrays Non Smoking Section last name and first name members
{
printf("\t%s %s\t", all[x].plname,all[x].pfname);
x++;
}
printf("\n");
}
/*************************************************************************************/
/************** This section prints the second header for the Smoking Section ***********************/
printf("\n\n");
for (x = 0; x<80; x++)
printf("*"); //Prints the asterisks in the heading
printf("\n");
for (x = 0; x<6; x++)
printf("*");
printf("\n");
printf("*\t\t\tSmoking Section\n");
printf("*\n");
for (x = 0; x<80; x++)
printf(".");
/******************** Prints the Smoking Section Seating Assignments ********************************************/
for(x=0;x<10;x++)
{
for(y=0;y<3;y++) //print tests output for plane array
printf("%c ",plane[x][y]);
printf("\n");
}
x=0;
for(a=0;a<10;a++)
{
for(b=0;b<3;b++) //print test for all array of structures
{
printf("%s %s\t", all[x].pfname,all[x].plname);
x++;
}
printf("\n");
}
printf("\n");
x=0;
for(a=8;a<11;a++)
{
for(b=0;b<3;b++)
{
printf("\t%s %s\t", assign[x].plname,assign[x].pfname);
x++;
}
printf("\n");
}
/********************** This section prints the third header for the Waiting List *******************************/
printf("\n\n");
for (x = 0; x<80; x++)
printf("*"); //Prints the asterisks in the heading
printf("\n");
for (x = 0; x<6; x++)
printf("*");
printf("\n");
printf("*\t\t\t\tWaiting List\n");
printf("*\n");
printf("*\tNAME\t\tSMOKING CHOICE\t\tROW\t\tCOLUMN\n");
printf("*\n");
for (x = 0; x<80; x++)
printf(".");
/****************************** Prints the Waiting List Info ********************************************************/
printf("\n");
x = 0;
for(a=0;a<11;a++)
{
printf("%i\t%s %s\t\t%c\t\t%i\t\t%i",(a+1),waiting[x].plname,waiting[x].pfname,waiting[x].stPref,waiting[x].row,waiting[x].col);
x++;
printf("\n");
}
/************************************ End of Function *************************************************/
}
void seatPrgm()
{
a=0;
z=0;
for(x=0;x<10;x++)
{
for(y=0;y<3;y++) //FIX THIS!!!!!!!!!!!!!!!!
{
while((plane[x][y]) == 'E') //While empty
{
if((all[z].stPref)=='N')
{
(all[z] = assign[a+1]); //Fills the assigned seats
(plane[x][y] == 'F');
z++;
a++;
}
else
{
(all[z] = assign[a+1]);
(plane[x][y] == 'F');
z++;
a++;
};
};
while(plane[x][y] == 'F') //Puts them on the waiting list if that seat is already occupied or 'F' FULL.
{
while(z<41)
{
(all[z] = waiting[z]);
z++;
a++;
};
};
};
};
}

Related

scanf from console and fscanf from file

I got some problems about scanf from console,
I wrote some codes about it and found that it could not read all the input and automatically close after typing first character.
int readLetterGridFromConsole(char letterGrid[MAX_GRID][MAX_GRID]) {
int row, col;
int gridSize;
printf("Enter the size of the letter grid:\n");
scanf("%d", &gridSize);
if(gridSize < 2 || gridSize > MAX_GRID) {
printf("DEBUG: gridSize is %d but not between 2 and %d\n", gridSize, MAX_GRID);
printf("Program terminates.\n");
exit(1);
} else {
printf("Enter the letter grid:\n");
for(row = 0; row < gridSize; row++) {
for(col = 0; col < gridSize; col++) {
scanf(" %c", &letterGrid[row][col]);
}
}
return gridSize;
}
}
I would like to store these input in the 2D array ( letterGrid )
how could I solve this problem? Thanks for help!
But it works for me.
I define a 2-D array char x[MAX_GRID][MAX_GRID]; then pass it to function, then scan input then store them in the x matrix.
Code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX_GRID 2
int readLetterGridFromConsole(char letterGrid[MAX_GRID][MAX_GRID]) {
int row, col;
int gridSize;
printf("Enter the size of the letter grid:\n");
scanf("%d", &gridSize);
if(gridSize < 2 || gridSize > MAX_GRID) {
printf("DEBUG: gridSize is %d but not between 2 and %d\n", gridSize, MAX_GRID);
printf("Program terminates.\n");
exit(1);
} else {
printf("Enter the letter grid:\n");
for(row = 0; row < gridSize; row++) {
for(col = 0; col < gridSize; col++) {
scanf(" %c", &letterGrid[row][col]);
}
}
//return gridSize;
}
/*
printf("Lettr grid\n\n");
for(row = 0; row < gridSize; row++) {
for(col = 0; col < gridSize; col++) {
printf("%c", letterGrid[row][col]);
}
}
*/
}
int main()
{
char x[MAX_GRID][MAX_GRID];//={{1,2},{3,4}};
readLetterGridFromConsole(x);
printf("after function\n\n\n");
for(int row = 0; row < MAX_GRID; row++) {
for(int col = 0; col < MAX_GRID; col++) {
printf("%c", x[row][col]);
}
}
return 0;
}
Compile and Run
gcc -Wall -Wextra -pedantic-errors code.c -o code
Output
Enter the size of the letter grid:
2
Enter the letter grid:
a
b
c
d
after function
abcd
Consider as array in c pass by reference it can hold values even outside of function readLetterGridFromConsole(x);

BIG WEIRD ANSWER in a MATRIX multiplication program

this is a program to multiply 2 matrixes which comes in maths, but dont know why, am getting answers like "-1282230" or some weird numbers. I would like to know what is causing it and how could i fix it? THANK YOU!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
int m[3][3],m2[3][3],i,je,k,ans[3][3],sum;
// taking inputs from the user for matrix1
printf("Enter the numbers for first matrix");
je=0;
for(i=0;i<3;i++){
printf(" for row %d\n",i+1);
for(je=0;je<3;je++){
scanf("%d",&m[i][je]);
}
}
// taking inputs from the user for matrix2
printf("Enter the numbers for second matrix");
je=0;
for(i=0;i<3;i++){
printf(" for row = %d\n",i+1);
for(je=0;je<3;je++){
scanf("%d",&m2[i][je]);
}
}
// multiplication OR MATRIX CMS HERE;
sum = 0;
for(k=0;k<9;k++){
for(i=0;i<3;i++){
for(je=0;je<3;je++){
sum = m[k][je] * m2[je][i];
ans[i][je] = sum;
}
}
k++;
}
// it ENDS;
puts("ANSWER IS:: \n");
// Displaying answer, matrix;
for(i=0;i<3;i++){
for(je=0;je<3;je++){
printf("%d\t",ans[i][je]);
}
printf("\n");
}
return 0;
}
Here's a working solution. One of the problem with your code is that you aren't setting the sum to 0 after each multiplication.
#include <stdio.h>
int main() {
int m, n, p, q, c, d, k, sum = 0; int first[10][10], second[10][10], multiply[10][10];
printf("Enter number of rows and columns of first matrix\n"); scanf("%d%d", &m, &n); printf("Enter elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter number of rows and columns of second matrix\n"); scanf("%d%d", &p, &q);
if (n != p)
printf("The matrices can't be multiplied with each other.\n"); else {
printf("Enter elements of second matrix\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0; }

Star Pattern Logic

I want make a star pattern just like this.
*
* * *
* * * * *
* * *
*
I am using this code.
for (int i=1; i<=3; i++) {
for (int j=1; j<=3-i; j++) {
printf(" ");
}
for (int k=1; k<=2*i-1; k++) {
printf("x");
}
printf("\n");
}
This will give
*
* * *
* * * * *
How I will iterate in decrement order.
when I am using
for (int i=3; i>=0; i--) {
for (int j=1; j<=3-i; j++) {
printf(" ");
}
for (int k=1; k<=2*i-1; k++) {
printf("x");
}
printf("\n");
}
xxxxx
xxx
x
How we merged it?
Another logic here
int x=11;
int y=x/2;
int z=1;
BOOL b1=true;
BOOL b2= true;
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
printf(" ");
}
for(int k=0;k<z;k++)
{
printf("*");
}
if(y==0) b1=false;
if(z==x) b2=false;
y=b1?y-1:y+1;
z=b2?z+2:z-2;
printf("\n");
}
Try with below logic,
for (int i=1; i<=3; i++) {
for (int j=1; j<=3-i; j++) {
printf(" ");
}
for (int k=1; k<=2*i-1; k++) {
printf("*");
}
printf("\n");
}
for (int i =2; i>=0; i--) {
for (int j=1; j<=3-i; j++) {
printf(" ");
}
for (int k=1; k<=2*i-1; k++) {
printf("*");
}
printf("\n");
}
Output Will look like :
*
***
*****
***
*
The given pattern is a combination of simple pyramid star pattern and inverted pyramid star pattern. It consist of N*2-1 rows (for this case N=3). Each row contain spaces and stars in printed in increasing and decreasing order.
Stars are printed in increasing order till Nth row. After Nth row stars are printed in decreasing order.
Spaces are printed in decreasing order till Nth row. After Nth row spaces are printed in increasing order. Point your mouse cursor over the pattern to count total spaces.
Step by step descriptive logic to print diamond star pattern.
Input number of rows to print from user (in real number of rows/2). Store it in a variable say rows.
Declare two variables to keep track of total columns to print each row, say stars=1 and spaces=N-1.
To iterate through rows, run an outer loop from 1 to rows*2-1. The loop structure should look like for(i=1; i<rows*2; i++).
To print spaces, run an inner loop from 1 to spaces. The loop structure should look like for(j=1; j<=spaces; j++). Inside this loop print single space.
To print stars, run another inner loop from 1 to stars*2-1. The loop structure should look like for(j=1; j<=stars; j++). Inside this loop print star.
After printing all columns of a row, move to next line i.e. print new line.
Check if(i < rows) then increment stars and decrement spaces. Otherwise increment spaces and decrement stars.
Try Below code:-
int i, j, rows;
int stars, spaces;
printf("Enter rows to print : ");
scanf("%d", &rows);
stars = 1;
spaces = rows - 1;
/* Iterate through rows */
for(i=1; i<rows*2; i++)
{
/* Print spaces */
for(j=1; j<=spaces; j++)
printf(" ");
/* Print stars */
for(j=1; j<stars*2; j++)
printf("*");
/* Move to next line */
printf("\n");
if(i<rows)
{
spaces--;
stars++;
}
else
{
spaces++;
stars--;
}
}

how to return longest words from an array?

given 2 arrays wrds[] , chars[] as an input to a function such that
wrds[] = [ "abc" , "baa" , "caan" , "an" , "banc" ]
chars[] = [ "a" , "a" , "n" , "c" , "b"]
Function should return the longest word from words[] which can be constructed from the chars in chars[] array.
for above example - "caan" , "banc" should be returned
Note: Once a character in chars[] array is used, it cant be used again.
eg: words[] = [ "aat" ]
characters[] = [ "a" , "t" ]
then word "aat" can't be constructed, since we've only 1 "a" in chars[].
There are kinds of anwers online but they are not written in Objective C.Can anyone help me solve this question in OC?
First, walk through the word array, one word at a time, throwing out all the words that can't be formed from the second array. To do that, for each word, walk through the characters of the word, throwing out that character from the second array. If we come to a character that's not in the second array, that word can't be formed from those characters.
Now we have an array consisting solely of the words that can be formed from those characters. Now sort that array by word length, longest first. Now start walking the array, looking at the length of each word. When that length value changes, stop; you have found all the longest words.
// Program do to do the same in C++
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
using std::vector;
vector<char*> match(char** words, int size, char* chars, map<char,int> &second)
{
vector<char*> res;
std::map<char,int> mapi = second;
int currsize = 0;
for(int i = 0; i < size ; i++){
char* wo;
wo = words[i];
int s= 0;
for( s=0; wo[s] != '\0'; s++){
}
if(s < currsize) {
//No need to iterate if already found a bigger word
//continue to see if the next word if bigger of the same size as currsize
continue;
}
// iterate through the map to see if all the letters present in the first array
bool found = true;
for(int j = 0; j <s ; j++){
map<char, int>::iterator it = mapi.find(wo[j]);
if(it == mapi.end()) {
found= false;
break;
}
}
if(!found) {
continue;
}
if(s > currsize) {
//remove the past res as found a bigger one
res.clear();
}
//Store this word in the vector as it is one of the biggest word so far
res.push_back(wo);
currsize = s;
}
return res;
}
int main()
{
map<char, int> leters;
char* words[5] = {"adc", "baa", "caan", "daanns", "banc"};
char ch1[]= {'a', 'a', 'n', 'c', 'b'};
int chsize = sizeof(ch1);
// put the ch1 chars in a map
for(int i = 0; i < chsize; i++) {
map<char,int>::iterator it =leters.find(ch1[i]);
if(it != leters.end()) {
it->second = it->second+1;
} else {
leters.insert(make_pair(ch1[i], 1));
}
}
char* chars = ch1;
vector<char*>v = match(words, 5, ch1, leters);
for(vector<char*>::iterator it = v.begin(); it != v.end(); it++) {
// it will print the result
cout << *it << endl;
}
return 0;
}

variable argument function

while doing a program related to variable argument function i
got the header file stdarg.h and have done some simple problem using it
but now when i a changing the actual argument's type it is showing some weird behaviour
here is my code:
#include<stdio.h>
#include<stdarg.h>
void fun(int a,...)
{
va_list k;
va_start(k,a);
int i=0;
printf("%d ",a);
while((i=va_arg(k,int)!=0)
{
printf(" %d ",i);
}
va_end(k);
}
int main()
{
fun(1,2,3,4,5,6);
printf("\n");
fflush(); //and without flush it is also showing some extra garbage value
fun(2,4,5);
printf("\n");
fflush();
fun('c','f','g','l');
return 0;
}
If you use the integer value 0 to indicate end of the argument list, you should also pass a 0 to fun.
fun(1,2,3,4,5,6,0);
fun(2,4,5,0);
First, end your list with 0, because you check for !=0 to detect the end. And also:
while((i=va_arg(k,int))!=0)
instead of
while(i=va_arg(k,int)!=0)
!= has higher precedence than =
This will give you the expected output:
1 2 3 4 5 6
Here's the complete code:
#include<stdio.h>
#include<stdarg.h>
void fun(int a,...)
{
va_list k;
va_start(k,a);
int i=0;
printf("%d ",a);
while((i=va_arg(k,int))!=0)
{
printf(" %d ",i);
}
va_end(k);
}
int main()
{
fun(1,2,3,4,5,6,0);
printf("\n");
//fun(2,4,5,0);
printf("\n");
//fun('c','f','g','l','\0');
getch();
return 0;
}