Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm still learning the basics of C and I had this assignment of coding a program that shows the product of two 2D arrays entered by the user. The idea is to not make use of memory functions like malloc.
The code worked fine but when I added an if statement to check that the rows of the first array are equal to the columns of the next one during the run-time the whole part for entering the data of the first array and showing it is skipped and it goes directly to asking for the next array. I've tried to clean the buffer and use getch() trying to force the compiler to execute that part but it keeps being skipped.
Here's the whole code :
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
int main(void);
int main (void)
#define MAXR 36
#define MAXC 18
{
int Rows_A,Columns_A,Rows_B,Columns_B,Rows_C,Columns_C;
int Counter_Rows=0,Counter_Columns=0,Counter_Multiplier=0;
int Matrix_A[MAXR][MAXC],Matrix_B[MAXR][MAXC],Results_Matrix_C[MAXR][MAXC];
printf("\n\tTo multiply two matrices both have to be the same size of rows from the first matrix and the columns from the next one. \n\tPlease enter the size of the matrices:\n");
printf("\n\tMatrix A:\n\tNumber of rows :\t\t");
scanf(" %d",&Rows_A);
printf("\n\tNumber of columns :\t\t");
scanf(" %d",&Columns_A);
printf("\n\tMatrix B:\n\tNumber of rows :\t\t");
fflush(stdin);
scanf(" %d",&Rows_B);
printf("\n\tNumber of columns :\t\t");
scanf(" %d",&Columns_B);
if (Rows_A=!Columns_B)
{
printf("\n\tMatrix B number of columns and Matrix A number of rows are not the same therefore they cannot be multiplied.");
return 0;
}
else
{
Rows_C=Rows_A;
Columns_C=Columns_B;
printf("\n\tEnter the data for Matrix A:");
for (Counter_Rows=0;Counter_Rows < Rows_A;Counter_Rows++)
{
for(Counter_Columns=0;Counter_Columns < Columns_A; Counter_Columns++)
{
printf("\n\tEnter the value of the position [%d][%d] of the matrix A: ",Counter_Rows+1,Counter_Columns+1);
scanf(" %d",&Matrix_A[Counter_Rows][Counter_Columns]);
}
}
printf("\n\tMatrix A.\n\t");//Matrix A
for (Counter_Rows= 0; Counter_Rows < Rows_A; Counter_Rows++)
{
for (Counter_Columns= 0; Counter_Columns < Columns_A; Counter_Columns++)
{
printf("[%d] ",Matrix_A[Counter_Rows][Counter_Columns]);
}
printf("\n\t");
}
fflush(stdin);
printf("\n\tEnter the data for Matrix B:");
for (Counter_Rows=0;Counter_Rows < Rows_B;Counter_Rows++)
{
for(Counter_Columns=0;Counter_Columns < Columns_B; Counter_Columns++)
{
printf("\n\tEnter the value of the position [%d][%d] of the matrix B: ",Counter_Rows+1,Counter_Columns+1);
scanf(" %d",&Matrix_B[Counter_Rows][Counter_Columns]);
}
}
printf("\n\tMatrix B.\n\t");//Matrix B.
for (Counter_Rows= 0; Counter_Rows < Rows_B; Counter_Rows++)
{
for (Counter_Columns= 0; Counter_Columns < Columns_B; Counter_Columns++)
{
printf("[%d] ",Matrix_B[Counter_Rows][Counter_Columns]);
}
printf("\n\t");
}
//Calculating product matrix C.
for (Counter_Rows=0;Counter_Rows < Rows_C;Counter_Rows++)
{
for(Counter_Columns=0;Counter_Columns < Columns_C; Counter_Columns++)
{
//initializes the Matrix C in 0's
Results_Matrix_C[Counter_Rows][Counter_Columns]=0;
for(Counter_Multiplier=0; Counter_Multiplier < Columns_A; Counter_Multiplier++)
{
Results_Matrix_C[Counter_Rows][Counter_Columns]=Results_Matrix_C[Counter_Rows][Counter_Columns]+(Matrix_A[Counter_Rows][Counter_Multiplier]*Matrix_B[Counter_Multiplier][Counter_Columns]);
}
}
}
printf("\n\tProduct Matrix C:\n\t");//Matrix B.
for (Counter_Rows= 0; Counter_Rows < Rows_C; Counter_Rows++)
{
for (Counter_Columns= 0; Counter_Columns < Columns_C; Counter_Columns++)
{
printf("[%d] ",Results_Matrix_C[Counter_Rows][Counter_Columns]);
}
printf("\n\t");
}
}
return 0;
}
This is the part that is skipped :
printf("\n\tEnter the data for Matrix A:");
for (Counter_Rows=0;Counter_Rows < Rows_A;Counter_Rows++)
{
for(Counter_Columns=0;Counter_Columns < Columns_A; Counter_Columns++)
{
printf("\n\tEnter the value of the position [%d][%d] of the matrix A: ",Counter_Rows+1,Counter_Columns+1);
scanf(" %d",&Matrix_A[Counter_Rows][Counter_Columns]);
}
}
printf("\n\tMatrix A.\n\t");//Matrix A
for (Counter_Rows= 0; Counter_Rows < Rows_A; Counter_Rows++)
{
for (Counter_Columns= 0; Counter_Columns < Columns_A; Counter_Columns++)
{
printf("[%d] ",Matrix_A[Counter_Rows][Counter_Columns]);
}
printf("\n\t");
}
you have if (Rows_A=!Columns_B) in your example. I think you want if (Rows_A != Columns_B)
Related
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 1 year ago.
Improve this question
I am working on an exercise:
You have a matrix written in a file and given some commands you have to shift
(left or right) or (up or down) in a circular way a certain row or column and after given the instructions you print it out.
This is my code so far. The problem is that it is like the switch operator is not reading anything. It is just printing "Exiting" which is in the default block.
I know that it is far from done but I just need a bit of help with my problem.
To be clear, I only need help with why the switch statement isn't working. I want to try to solve the rest of the problem myself. I just need help with that specific part.
#include <stdio.h>
#include <stdlib.h>
#define N 30
int main()
{
int v[N][N];
int i=0,p,j=0;
int command1;
int command2;
int times;
int times2=0;
FILE*file;
file=fopen("file","r");
if(file==NULL)
{
printf("ERROR in the file....");
exit(1);
}
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
fscanf(file,"%d ",&v[i][j]);
}
}
printf("Original Matrix: \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%d ",v[i][j]);
}
printf("\n");
}
printf("\n\n");
printf("Choose:\n1:Rows\n2:Columns\n3:Exiting\n");
{
scanf("%d",&command1);
}
switch (command1)
{
case '1':
{
printf("Choose:\n1:Shift left\n2:Shift right\n");
scanf("%d",&command2);
if(command2==1)
{
printf("Which row:\n");
scanf("%d",&p);
printf("\n");
printf("How many times:\n");
scanf("%d",×);
while(times2<times && i<3)
{
v[p][i]= v[p][i+1];
v[p][i+1]=v[p][i+2];
v[p][i+2]=v[p][i];
i++;
times2++;
}
printf("\nNew Matrix:\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%d ",&v[i][j]);
}
printf("\n");
}
}
}
break;
case '2':
{
printf("Something Something");
}
default:
printf("\nExiting");
}
return 0;
}
Make that case 1: and case 2: without any single quotes. When you use '1' and '2' it's checking for chars, but command1 is an int.
Make sure to add a break; statement to the second case as well so it doesn't fall through to the default: case.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am running a hollow box statement and cant figure out the error message
error: expected identifier or ‘(’ before ‘{’ token" {
I have tried multiple variations of the bracket in different positions and it just causes more errors. Here is the updated code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main ()
{
int i;
printf("Even numbers between 25 to 75: \n");
for (i = 25; i<=75; i++)
{
if(i%2 == 0)
{
printf("%d\t", i);
}
}
printf("All odd numbers between 500 to 400: \n");
for (i = 500; i>=400; i--)
{
if(i%2 == 0)
{
printf("%d\t", i-1);
}
}
int number, result, exponent;
result = 1;
printf("Enterthe base number: ");
scanf("%d", &number);
printf("Enter the exponent: ");
scanf("%d", &exponent);
while (exponent != 0)
{
result *= number;
--exponent;
}
printf("Answer = %d \n", result);
}
int f, w;
{
for (f = 1; f <= 7; f++)
{
for (w = 1; w <= 7; w++)
{
if (f==1 || f==7 || w==1 || w==7)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
This is an update to the first question since my first post didnt show the whole code and everyones answer was asking for it.
You should put an int main(void) before the body of your main function. You can then move your variables inside the function. After you've done this, the top of your code should look like:
int main(void) /* Here! */
{
int f, w; /* Move this inside the function. */
for (f = 1; f <= 7; f++)
...
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
This code aims to exclude non-repeating values in a number list, but does not return repeats in their original sequence. How to keep all repeats with their precedence?
My code:
#include <stdio.h>
#include <stdlib.h>
int count=0;
int main ()
{
int i,j,k,x, altarray[1000],array[1000];
printf("Please enter the number of integers in your list:\n");
scanf("%d",&x);
printf ("Please enter the list of numbers\n");
for (i=0; i<x; i++)
scanf("%d",&array[i]);
printf("\nThe corrected array is...\n");
for (i=0; i<x; i++)
{
for (j=i+1; j<x; j++)
{
if(array[i]==array[j])
{
altarray[count]=array[i];
count++;
}
}
}
for (i=0; i<count; i++)
printf("%d",altarray[i]);
}
To search for duplicated number, you have to cover all elements of your array in the j for loop except the one that have the index i. So that loop should look like this:
for (j = 0; j < x; j++)
{
if ((array[i] == array[j]) && (i != j))
{
altarray[count] = array[i];
count++;
break;
}
}
I have added a break, to avoid wasting cycles after detecting the existence of the duplicated number.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
For some reason when I reallocate the size of the array created using calloc it deletes the values that have already been inputted, maybe something else is happening but i don't understand why. I have changed the code so that it includes everything it needs to work, sorry i forgot about that
#include <stdio.h>
#include <stdlib.h>
int main(void) {
unsigned int arraySize; // size of array
int moreElements; // stores user input whether more elements are to be
int additionalElements = 0; // number of elements to be added to array
unsigned int type; // stores the type of array
float sum = 0; // the sum of the elements
float *floatArrPtr; // pointer to a flaot
floatArrPtr = calloc(arraySize, sizeof(float));
for(size_t i = 0; i < arraySize; i++)
{
printf("Please enter a number for the array: ");
scanf("%f", &floatArrPtr[i]);
printf("%f\n", floatArrPtr[i]);
}
for(size_t i = 0; i < arraySize; i++)
{
sum += *(floatArrPtr+i);
}
sum /= arraySize;
printf("The average of the elements of the array is %lf\n", sum);
do
{
printf("if there are more elements to be added enter 1 and 0 if not: ");
scanf("%d", &moreElements);
} while (moreElements != 0 && moreElements != 1);
if (moreElements == 1) {
printf("please enter the number of additional elements you want to add: ");
scanf("%d", &additionalElements);
floatArrPtr = realloc(intArrPtr,(arraySize+additionalElements) * sizeof(float));
for (size_t i = arraySize; i < arraySize+additionalElements; i++)
{
printf("Please enter a number for the array: ");
scanf("%f", &floatArrPtr[i]);
}
sum = 0;
for(size_t i = 0; i < arraySize+additionalElements; i++)
{
sum += *(floatArrPtr+i);
printf("%zu, %lf, %d\n", i, floatArrPtr[i], arraySize + additionalElements);
}
sum /= (arraySize + additionalElements);
printf("The average of the elements of the array is %lf\n", sum);
}
}
That calloc code at the top is wrong. For an arraySize of 1000 it allocates a million floats, or 4MB. Look that up.
Then I assume the real problem is the intArrayPtr that slipped in from earlier code.
Use functions, they pay off. – I mean make all your code no more than 4 lines or so long per function, this will stop old variables from earlier from slipping in.
The wrong line is
floatArrPtr = realloc(intArrPtr...
You need
floatArrPtr = realloc(floatArrPtr...
I don't know the purpose of intArrPtr, but it looks like if that code compiles that its coming in from code above.
You have global variables. One must be very careful with them as they are a pain at best, at worst they cause unforeseen edge case bugs, which is what you have.
Make your project two files, one for integer and one for float, and you will see your mistake in the compiler.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Here is my code. It will take the info from the user fine, but it doesn't call prims! (it doesn't even print the statement before the call..). The issue is, this main() is simply copy-and-pasted from an attempt at this problem using kruskals instead of prims.. the main is unchanged, and it used to work fine, the only difference is prims() is now there. I can't see any reason why the program would just.. stop (and then does nothing. Blinking cursor nothing). What's going on?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 50
typedef struct graph{
int vertices;
int edges;
int vertex[MAX];
int edge[MAX][4]; /*[i][0]=i (edge ref) [i][1]=vertex1 [i][2]=vertex2 [i][3]=weight*/
} Graph;
void prims(Graph graph);
int main () {
Graph* graph=malloc(sizeof *graph);
printf("Please enter the number of vertices in your graph: ");
scanf("%i", &graph->vertices);
printf("\nPlease enter the number of edges in your graph: ");
scanf("%i", &graph->edges);
for (int i=0; i<graph->edges; ++i) {
graph->edge[i][0]=i;
ensure_valid_input:
printf("\nPlease enter the vertices connected by edge %i, and its weight: ",i+1);
scanf("%i %i %i", &graph->edge[i][1], &graph->edge[i][2], &graph->edge[i][3]);
if (graph->edge[i][1]>graph->vertices || graph->edge[i][2]>graph->vertices || graph->edge[i][1] <= 0 || graph->edge[i][2] <= 0) {
printf("\nERROR: One of these vertices is invalid; ensure they are both in range 1 - %d\n", graph->vertices);
goto ensure_valid_input;
}
}
printf("Try to call the function?");
prims(*graph);
/*Print result to screen*/
/*Print result to file*/
return 0;
}
void prims(Graph graph){
printf("Function called...");
/*Initialise sets and test-values*/
int edges_ordered[graph.edges];
int used_vertices[graph.vertices];
int used_edges[graph.vertices-1];
int least_avail_edge;
int least_edge_reset;
int existing_entry;
int done=1;
int vertex_present;
for (int i=0; i<graph.vertices; ++i) {
if (i=0) {
used_vertices[i]=0;
}
else {
used_vertices[i]=-1;
}
}
/*Order the edges*/
for (int i=0; i<graph.edges; ++i) {
least_avail_edge = least_edge_reset;
existing_entry = 1;
for (int j=0; j<graph.edges; ++j) {
if (graph.edge[j][3]<=graph.edge[least_avail_edge][3]) {
for (int k=0; k<graph.edges; ++k) {
if (edges_ordered[k]==graph.edge[j][0]) {
existing_entry=0;
}
}
if (existing_entry==1) {
least_avail_edge=j;
}
}
}
edges_ordered[i]=least_avail_edge;
}
//Diagnotstic Print
for (int i=0; i<graph.edges; ++i) {
printf("\n%d) Edge %d, Weight %d\n)", i+1, edges_ordered[i]+1, graph.edge[i][3]);
}
/*Continually add next appropriate edge to tree until spanning*/
while (done!=0) {
/*Test to see if all vertices are in the tree yet*/
done=0;
for (int i=0; i<graph.vertices; ++i) {
vertex_present=1;
for (int j=0; j<graph.vertices; ++j) {
if (graph.vertex[i]==used_vertices[j]) {
vertex_present=0;
}
}
if (vertex_present==1) {
/*Vertex is missing from tree -- not done!*/
done=1;
break;
}
}
}
}
There are a lot of problems with this code, but I suspect the culprit is inside your prims() function:
if (i=0) {
I think you mean ==. You're setting i to zero each time through the loop, which causes the loop to never terminate, freezing your program.
Other problems are that least_edge_reset is uninitialized, the return value of malloc is not cast (depends on the C/C++ version and compiler settings whether you'll get an error or warning for this), and sizeof *graph is awkward. Also there's no protection against exceeding the maximum number of edges, etc. etc., but I'll stop there since it's not what you were asking about.
I suspect your print statement IS running, but since there is no \n, it is being buffered and not displayed on screen right away, and never gets displayed because prims() is stuck in an infinite loop.