How to save a matrix indexes [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Im practicing c and I have some trouble with this code :
I want to make a movie theatre ticket program.
The programs is getting a specific number evertime, and its seeking for the first free seats, if there are none it suppose to print a massage.
The seats must be attached to each other and in the same row.
The indexes of the seats need to be printed as well as mark as taken..
Im marking 1 as taken and 0 as untaken.
I need your help with how to save the indexes everytime.
#include <stdio.h>
#include <string.h>
main()
{
int i, j;
int Freeseats = 0;
int arr[10][20]= {{0}};
for(i=0; i<10; i++)
{
for(j=0; j<20; j++)
{
printf("%d", arr[i][j]);
}
printf("\n");
}
while(1)
{
int num, n=0;
scanf("%d",&num);
for(i=0; i<10; i++)
{
for(j=0; j<20; j++)
{
if(arr[i][j] == 0 && Freeseats < num)
{
Freeseats++;
}
else{
Freeseats = 0;
}
if(Freeseats == num)
{
}
}
}
}
}

I think your approach is wrong. You don't need to save any indexes.
Your task is to find a row where there is N consecutive free seats. So you need an extra loop where you count the number of consecutive free seats.
Something like:
#include <stdio.h>
#include <string.h>
#define SEATS_PER_ROW 20
#define ROWS 10
main()
{
int i, j, k, max;
int arr[ROWS][SEATS_PER_ROW]= {{0}};
for(i=0; i<10; i++)
{
for(j=0; j<20; j++)
{
printf("%d", arr[i][j]);
}
printf("\n");
}
while(1)
{
printf("Please enter the number of seats needed\n");
int num, n=0;
if (1 != scanf("%d",&num))
{
printf("Illegal input - stop program\n");
break;
}
printf("Searching for %d seats\n", num);
if (num > SEATS_PER_ROW)
{
printf("We don't have that many seats in a single row\n");
continue;
}
int found = 0;
for (i=0; i<10 && found != 1; i++)
{
found = 0;
for (j=0; j<(20-num+1) && found != 1; j++)
{
found = 0;
// Check for num consecutive free seats starting at i, j
for (k=0; k<num; k++)
{
if(arr[i][j+k] != 0)
{
// Can't sit here
found = -1;
break;
}
}
if (found == 0)
{
// We found a place - starting at i, j
// Mark them as used
found = 1;
printf("Seats assinged in row %d : seat %d to %d\n", i, j, j+num-1);
for(k=0; k<num; k++)
{
arr[i][j+k] = 1;
}
// Debug print
//for(i=0; i<10; i++)
//{
// for(j=0; j<20; j++)
// {
// printf("%d", arr[i][j]);
// }
// printf("\n");
//}
}
}
}
if (found != 1)
{
printf("So many seats are not available\n");
}
}
return 0;
}
With input:
10
15
2
6
20
3
21
9
The output is:
Please enter the number of seats needed
Searching for 10 seats
Seats assinged in row 0 : seat 0 to 9
Please enter the number of seats needed
Searching for 15 seats
Seats assinged in row 1 : seat 0 to 14
Please enter the number of seats needed
Searching for 2 seats
Seats assinged in row 0 : seat 10 to 11
Please enter the number of seats needed
Searching for 6 seats
Seats assinged in row 0 : seat 12 to 17
Please enter the number of seats needed
Searching for 20 seats
Seats assinged in row 2 : seat 0 to 19
Please enter the number of seats needed
Searching for 3 seats
Seats assinged in row 1 : seat 15 to 17
Please enter the number of seats needed
Searching for 21 seats
We don't have that many seats in a single row
Please enter the number of seats needed
Searching for 9 seats
Seats assinged in row 3 : seat 0 to 8
Please enter the number of seats needed
Searching for 15 seats
Seats assinged in row 4 : seat 0 to 14
See it at https://ideone.com/36zjuC

Related

transpose square matrix with pointer in C. wrong output

The goal is to print the transpose of the 'Matrix'.
To create square matrix, I got 'row' from the keyboard.
row is same with column so I only declared 'row'.
the problem I need help is right below ↓
/*input*/
5 4 1
9 0 1
6 5 7
/*output I want*/
5 9 6
4 0 5
1 1 7
/*wrong output I get*/
0 4 -30838770
0 7 2
0 5 7
And here is my code. Matrix in each function must be called by reference. I also want to know if I got it right.
//code start
int Generate(int row, int (*Matrix)[row])
{
srand(time(NULL)); //make random number
int i, j;
printf("Matrix = ");
for(i=0; i<row; i++){
for(j=0; j<row; j++){
Matrix[i][j] = (rand() % 10); //insert random number from 0 to 10
printf("%d ", Matrix[i][j]); //print matrix before transposing
}
printf("\n");
}
return 0;
}
void Transpose(int row, int (*Matrix)[row])
{
int i, j;
for(i=0; i<row; i++){
for(j=0; j<row; j++){
int transpose[i][j];
transpose[i][j] = Matrix[j][i];
printf("%d ", transpose[i][j]);
}
printf("\n");
}
}
int main() {
int input; //for switch case
int row = 0; //row has to be 2 or 3
int Matrix[row][row]; //2d array. largest index should be Matrix [row-1][row-1]
while(1){
scanf("%d", &input);
switch(input){
case 1: // Generate random square matrix
scanf("%d", &row); //insert row
Generate(row, Matrix);
break;
case 2: //transpose matrix
Transpose(row, Matrix);
break;
default:
return 0;
}
}
}
//code end
I'm new to this community so I'm not sure I gave you all the information needed.
Please let me know the lines you don't understand because I really want to get this code work.
I'm waiting for your help!
Your program is trying to print the transpose, so you don't need to store anything. remove all the stuff with transpose[i][j], and just print Matrix[j][i]. As pointed out in the comments, you are allocating (on the stack) a new matrix of shape ixj at each iteration, which makes no sense.

How to reverse the position of a nested loop output?

I'm a beginner C programmer and a college freshman.
I need a little help here with a test i'm working on here.
I want to make a nested loop that shows a sorted number. Sorta like this:
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15
21 20 19 18 17 16
22 23 24 25 26 27 28
... ... ... and so on, depending the limit of rows you input
I already tried to make a crude trial-and-error test code:
int i;
int j;
int limit;
int number1 = 1;
int number2 = 3;
int spesial = 0;
printf("Input limit : ");
scanf("%d", &limit);
for (i=1;i<=limit;i++)
{
for(j=1;j<=i;j++)
{
if (i%2==0)
{
printf("%d ", number2);
number2--;
}
else
{
printf("%d ", number1);
}
number1++;
}
if (i%2==0)
{
number2=(i*6)-i+(spesial*1);
spesial+=1;
}
printf("\n");
}
I managed to make it sorted to the 7th rows, but the rest are not..
help please...
I want to know if we could actually control the position of the output without sorta crude our way like this.
Also, sorry for my English... I'm not really from an English speaking country and this is my first time posting/question in this site.
Thank you for reading this lengthy question and I hope you have a good day and good night.
https://ideone.com/yCxpHo:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int rows;
int i, j;
int n = 0;
printf ("How many rows do you want? ");
if (scanf("%d", & rows) != 1 || rows < 1) return EXIT_FAILURE;
printf ("\n");
for (i = 1; i <= rows; ++ i) {
for (j = 0; j < i; ++ j) {
printf ("%4d", n + (i % 2 == 0 ? i - j : j + 1));
}
printf ("\n");
n = n + i;
}
return EXIT_SUCCESS;
}
It can be more convenient to create another function that will calculate the biggest number of a row (I called it lineMax).
int lineMax(int num){
int cnt=0;
for (int i=1;i<=num;i++)
cnt+=i;
return cnt;
}
void main(){
int i,j,limit;
printf("Input limit : ");
scanf("%d", &limit);
for(i=1;i<=limit;i++){
if(i%2==0){ //right to left
for(j=lineMax(i);j>=lineMax(i-1)+1;j--)
printf("%d ",j);
}
else{ //left to right
for(j=lineMax(i-1)+1;j<=lineMax(i);j++)
printf("%d ",j);
}
printf("\n");
}
}
You are making a lot of special cases with number1, number2 and special. This will not work for bigger numbers.
One way is to calculate count which will give you the value to start from in each loop of j. count += i and then every time print count -j
count = 0;
for (i=1;i<=limit;i++)
{
count += i;
for(j=0;j< i;j++)
{
printf ("%d ",count-j);
}
printf("\n");
}

how to display reserved cinema seats again in c programming

The following is the code I made for a seat reservation at the cinema. the problem is when I display seats again after reservation it's not displaying the reserved seats.
p.s: I can see the message "seat is already reserved but it is not displaying on the screen.
0=empty seats.
1=reserved seats.
THIS IS CODING:
#include <stdio.h>
void DisplaySeats(void);
void ReserveSeats(void);
void ChooseSeat(void);
int Seats[4][10];
int main()
{
printf("Welcome to our small Cinema!!!\n");
printf("\n");
DisplaySeats();
ReserveSeats();
printf("Thankyou for Choosing our small Cinema !! \n");
getch();
}
void ChooseSeat(void)
{
int row, col,k;
printf("Which row do you want to choose? : ");
scanf_s("%d", &row);
printf("Which seat do you want to select? : ");
scanf_s("%d", &col);
if (row > 4 || col > 10)
{
printf("Wrong Entry !! Try again\n");
ChooseSeat();
}
else if (Seats[row - 1][col - 1] != 0)
{
printf("Seat is already reserved try again !!\n");
ChooseSeat();
}
else
{
Seats[row - 1][col - 1] = 1;
printf("Congratulations!! Reservation Completed!!!\n");
DisplaySeats();
}
}
void ReserveSeats(void)
{
int NoOfSeats,i;
printf("How many seats do you want to reserve?\n");
scanf_s("%d", &NoOfSeats);
for (i = 1; i <= NoOfSeats; i++)
{
ChooseSeat();
}
}
void DisplaySeats(void)
{
int i, j;
int Seats[4][10] = { 0 };
printf("\t \t Seats\n");
printf("\t1 2 3 4 5 6 7 8 9 10\n");
for (i = 0; i < 4; i++)
{
printf("Rows %d: ", i + 1);
for (j = 0; j < 10; j++)
printf("%d ", Seats[i][j]);
printf("\n");
}
printf("\n");
}
The problem is in your DisplaySeats function. You have a local Seats[4][10] in your display function, which means your global variable Seats[4][10] will be hidden inside your DisplaySeats.
Every time you call DisplaySeats, you just show a brand new Seats, NOT the one you modified in your ChooseSeat function. So just get rid of int Seats[4][10] = { 0 }; in your DisplaySeats function , and you'll be fine.
I solved some years ago the same problem with the following idea:
each row of the cinema is a linked list and each element of the list contain an address of a new linked list for each column.
For the persistence of the information use file.txt
You can see it in my repository on github:
https://github.com/LombardoAndrea195/Sistemi_Operativi_Project/tree/master/Sistemi_Operativi_Lombardo

Incorrect result checking for Fisher numbers [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Program Details: Fisher Number
A Fisher number is an integer for which the product of its factors (including itself) is equal to the number's cube. For example, 12 is a Fisher number, because 123 = 2 x 3 x 4 x 6 x 12.
Example:
Input: 12
Output: true (12<sup>3</sup> = 2 x 3 x 4 x 6 x 12)
Input: 8
Output: false (8<sup>3</sup> != 2 x 4 x 8)
Objectives:
Write a program to check if the user input is a Fisher number or not.
Print all the Fisher numbers in a given range
Here is my code:
#include <stdio.h>
#include <conio.h>
int ch, product = 1, cube, num, i, j, min, max;
// Check Function
void check() {
int i;
printf("Enter the Number to check whether It is Fisher or Not\n");
scanf("%d", &num);
cube = num * num * num;
for(i = 2; i <= num; i++) {
if(num % i == 0) {
product = product * i;
}
}
if(cube == product) {
printf("It is a Fisher Number!\n");
}
else {
printf("It is not a Fisher Number\n");
}
}
// RANGE FUNCTION
void range() {
printf("Enter the Minimum and Maximum value of Range\n");
scanf("%d%d", &min, &max);
for(i = min; i <= max; i++) {
cube = i * i * i;
for(j = 1; j <= i; j++) {
if(i % j == 0) {
product = product * i;
}
}
if(cube == product) {
printf("%d\n", i);
}
}
}
void main() {
clrscr();
printf("Enter Your Choice \n");
printf("1 - Check Fisher Number \n");
printf("2 - Display Fisher Numbers in a Range \n");
scanf("%d", &ch);
switch(ch) {
case 1: check();
break;
case 2: range();
break;
default: printf("Enter Valid Choice \n");
}
getch();
}
Output picture and text:
Enter Your Choice
1 - Check Fisher Number
2 - Display Fisher Numbers in a Range
2
Enter the Minimum and Maximum value of Range
1 40
1
I am not getting the range output right. For example, if the range is set to 1 to 15, it only shows 1 which is wrong!
You should multiply by j here:
if(i%j == 0) {
product = product * i;
}
It would be more obvious if you used more meaningful variable names than i and j.
You also need to reset product variable before the inner loop (why is it a global variable anyway? Don't you know they are evil?)
cube = i * i * i;
product = 1;
for(j=1;j<=i;j++) { ...

Magic Square 2D array

I am tasked with creating a C program that will read a .txt file, convert it into a 2D array, and test whether or not it is a magic square (meaning that all columns, rows, and diagonals add up to [n(n^2 + 1)] / 2), where n is the number of integers in the row, column, or diagonal. The txt file contains 3 4x4 squares, 3 sets of 16 and the last number is zero. So the magic sum is 34. I have gotten the rows to add up, but I can't seem to figure out the columns or the diagonals. Also, I feel that with the way my code is going right now, it will only read the file once, and cannot read the other two 4x4 sets. Here is my code (sorry, it's pretty sloppy right now. I've been wrestling with this for a while, so it got a little messy)
int main (){
int array1[4][4], array2[4][4], array3[4][4], number=1, row, col, diag;//I messed with adding new arrays to send to functions, but it doesn't really do anything
FILE *file1;
file1 = fopen ("testdata.txt", "r");
if(file1!=NULL){
printf(" Square Perfect?\n----------------------------\n");
row=testRows(file1, array1, number);
col=testCols(file1, array3, number);
number++;
}
else
printf("File could not be read.\n");
return 0;
}
int testRows (FILE *file1, int array1[4][4], int number){
int i, j=0, r=0, q, tru=9000, array2[4][4], input;
if (file1!=NULL){
for(q=0; q<4; q++){
j=0;
r=0;
for (i=0; i<4; i++){
fscanf(file1, "%d", &array1[i][j]);
r=r+array1[i][j];//sum of rows
}
j++;
if (r!=34){
tru=0;
break;
}
if (r==34){
tru=1;
}
}
}
return tru;
}
int testCols(FILE *file1, int array1[4][4], int num){ //this is the problematic function
int i, j=0, r=0, q, tru=9000, array2[4][4], input;
if(num==1){//ignore this
file1 = fopen("testdata.txt", "r");//Before I added this, this function would read the next 16 numbers in the file and read their rows, when I need them to read the first 16 and their columns. I feel like this will really hurt when I need the function to read further into the file. Any suggestions?
}
if (file1!=NULL){
for(j=0; j<4; j++){ r=0;
for(i=0; i<4; i++){
fscanf(file1, "%d", &array1[i][j]);
printf("%d ", array1[i][j]);
r+=array1[i][j];// sum
printf(" ~%d~ ", r); //this is so I can see what the sum currently is. For some reason, it will only add up rows and not columns.
}
printf("\n");
}
//^^^^^ I have moved i's and j's around all over the place, but no matter what, it will print and add the sum of each row.
}
// return tru; variable tru is to return to main whether or not the square is magic; have not gotten that far yet.
}
/*int testDiag(){
}
*/
Thank you, any help would be greatly appreciated
Here is testdata.txt:
1 15 14 4 10 11 8 5 7 6 9 12 16 2 3 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 3 4 5 6 7 12 8 9 15 7 9 4 3 12 13
0
UPDATE!
I redid the entire program, works perfectly now. Take a look:
int main(){
int i, j, q, row, col, diag, array1[4][4], magic, magic1, magic2, w, sqn=1;
FILE *file1;
file1= fopen("testdata.txt", "r");
printf(" Square Magic?\n --------------------------------------\n");
for(w=0; w<3; w++){
if(file1!=NULL){
for(i=0; i<4; i++){
for(j=0; j<4; j++){
fscanf(file1, "%d", &array1[i][j]);
}}
}
for(i=0; i<4; i++){
row=0;
for(j=0; j<4; j++){
row+=array1[i][j];
}
if(row==34){
magic=1;
}
if(row!=34){
magic=0;
break;
}
}
for(i=0; i<4; i++){
col=0;
for(j=0; j<4; j++){
col+=array1[j][i];
}
if(col==34){
magic1=1;
}
if(col!=34){
magic1=0;
break;
}
}
for(i=0; i<4; i++){
diag=0;
for(j=0; j<4; j++){
diag+=array1[j][i];
}
if(diag==34){
magic2=1;
}
if(diag!=34){
magic2=0;
break;
}
}
printf(" Square %d ", sqn);
sqn++;
if(magic==1 && magic1==1 && magic2==1){
printf(" Magic!\n");
}
else{
printf(" Not Magic\n");
}
}
fclose(file1);
return 0;
}

Resources