I've written a program which carries out matrix multiplication using functions. The function which i presume is wrong is as follows:
void obtainMatrixElems(int mtrx[][10], int row_elems, int col_elems){
printf("Kindly enter matrix elements: \n");
for(int x = 0; x < row_elems; x++){
for(int y = 0; y < col_elems; y++){
printf("Enter element at position %d,%d: \n", x+1, y+1);
scanf("&d", &mtrx[x][y]);
}
}
}
It seems you are missing a "+" at multAns[x][y] = matrix1[x][y] * matrix2[x][y];.
It should rather be:
// Also note the change variables used for referencing cells ...
multAns[x][y] += matrix1[x][z] * matrix2[z][y];
In case your last operation result is 0 then this explains why you get a 0 matrix..
EDIT:
The sign is one of the problems .. There is also something wrong with the way you get input from the user.
for(int x = 0; x < row_elems; x++){
for(int y = 0; y < col_elems; y++){
printf("Enter element at position %d,%d: \n", x+1, y+1);
// Note the change of "&" to "%" and the extra sequence
// "\r\n" which expects the user to press ENTER (i.e.:
// new line) between input cells
rc = scanf("%d\r\n", &mtrx[x][y]);
if (rc != 1) {
printf("ERROR: scanf did not proceed as expected\r\n");
}
}
}
There is an error in your call to scanf
It should read
scanf("%d", &mtrx[x][y]);
Also take care with hitting the Enter Key after each input. To be safe you should catch it. Use something like
scanf(" %d", &mtrx[x][x]);
Related
I am trying to subtract a given number from an array and then store the results in a completely different array. Is it possible to write the code without using pointers?
I am trying to write the code with using for loop and or do/while loop.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(){
int num[100];
int i ;
int size;
int sub;
int diff[100];
printf("Enter the size of the array: ");
scanf("%d", &size);
for(i=0;i<size; i++){
printf("Enter the element %d :", i+1);
scanf("%d", &num[i]);
}
printf(" Enter the number to substract: \n");
scanf("%d", &sub);
for (i=0;i<size; i++)
{
y = num[i]- sub;
scanf("%d", &diff[y]);
}
for (y=0; y<size; y++)
{
printf("%d", diff[y]);
}
}
After I scan the results, I tried different ways to initialize and store the values in the second array but haven't been successful. What mistake am I making here?
y = num[i] - sub;
This is fine, as it's the result of subtraction for a given source array element.
scanf("%d", &diff[y]);
This doesn't make sense, as it's attempting to read input from the user. Not only that, it's using the result of the subtraction as the index of the destination array.
Just assign the result of the subtraction to the corresponding destination array member:
diff[i] = num[i] - sub;
In your question, you try to scan the value to another array, but the correct form is to assign the value in the new array position.
For example, in your first for loop use the i variable as the position and assign num[i] - sub on diff[i]:
for (i = 0; i < size; i++)
{
diff[i] = num[i] - sub;
}
instead of:
for (i=0;i<size; i++)
{
y = num[i]- sub;
scanf("%d", &diff[y]);
}
Hi I just completed coded my project, but when I trying to compile I kept getting this error. Which is '<=': 'int' differs in levels of indirection from 'int *', i don't know what is happening, and I already check for the pointer and already put it when defining variable type and still cannot compile it safely.
#define _CRT_SECURE_NO_WARNINGS
/* C Program to find Shortest Distances or Path */
#include<stdio.h>
#define WORD 200
int main(void) {
int path, point = 1;
int* ncity[WORD];
char* cityname[WORD][WORD];
float distance[WORD][WORD];
float total[WORD];
printf("This program will input 5 path and calculates the minimum between KL Sentral, KL & Jurong East, Singapore");
printf("\n\t\t-------------------------------------------------------------");
printf("\n\t\tMinimum Path between KL Sentral, KL & Jurong East, Singapore");
printf("\n\t\t-------------------------------------------------------------");
printf("\nPlease enter the total path that you want to calculate: ");
scanf("%d", &path);
for (int i = 1; i <= path; i++) {
printf("\n\n----Path %d----", i);
printf("\nState the number of city that the path cross: ");
scanf("%d", ncity[i]);
for (int x = 1; x <= path; x++) {
for (int y = 1; y <= ncity[i]; y++) {
printf("\nCity %d named : ", y);
scanf("%s", &cityname[x][y]);
printf("\nEnter the distance to the city %d: ", y);
scanf("%f", &distance[x][y]);
total[x] = +distance[x][y];
}
}
}
//Find the minimum path
for (int x = 1; x <= path; x++) {
if (total[x] < total[point]) {
point = x;
}
}
printf("\nThe minimum path between KL Sentral, Kuala Lumpur & Jurong East, Singapore");
printf("\nPath: Path %d", point);
printf("\nTotal Distance: %f", total[point]);
printf("\n\t\tCity Name");
//Loop for 42
for (int z = 1; z <= ncity[point]; z++) {
for (int x = 1; x <= path; x++) {
for (int y = 1; y <= ncity; y++) {
printf("\n\t\t %s", cityname[x][y]);
printf("\n\t\t %f km", distance[x][y]);
}
}
}
}
This the error that had been listed once i started compiling my code
By using z <= ncity[point] inside your loop you compare 'z' to the actual number of the address where your ncity[pointer] value is stored. Usually it is a big 'int' value. I can't get why you need an array of pointers here, but to get the first value of the ncity[point] you need to derefence that: *ncity[pointer]. The same happening inside your third loop: y <= ncity. You compare 'y' variable with the number of address of your ncity variable. Again, pretty big number, so most likely 'y' variable will be outside of 'cityname' and 'distance' and then you will get 'segmentation fault' error. To get the whole size of 'ncity' you need to iterate though all values or just use path valiable inside your third loop
for (int y = 1; y <= path; y++) {
printf("\n\t\t %s", cityname[x][y]);
printf("\n\t\t %f km", distance[x][y]);
}
I've been stuck on a problem for awhile, I need to read input from the user in the form,
5 1.2 2.3 3.4 4.5 5.6
where the first integer is the number of floats to expect, and then the floats following are the values I need to store in an array of that size. The code I have that keeps returning an error is,
...
int i = 0, j, k;
float value, *ptr;
// For every element in inputArr...
while (i < inputLength) {
printf("Enter the number of values in this data set, followed by the values: ");
// Get the int value for array creation...
scanf("%d ", &j);
printf("%d", j);
// Save it for the calculations later.
*(lengths + i) = j;
// Create dynamic array of floats.
*(inputArr + i) = calloc(j, sizeof(float));
ptr = *(inputArr + i);
// For the rest of the input read the floats and place them.
k = 0;
while (k < j-1) {
scanf("%f ", &value);
*(ptr + k) = value;
k++;
}
scanf("%f\n", &value);
*(ptr + j - 1) = value;
i++;
}
This throws a segmentation fault when I enter in the input above.
Can someone help me out by telling me what I'm doing incorrectly?
You do not have to include spaces and end-of-strings in your scanf calls.
scanf("%d", &j);
i/o
scanf("%d ", &j);
scanf("%f", &value);
i/o
scanf("%f\n", &value);
etc.
I am a beginner. I'm trying to solve a pair-sum program. The program calls for the number of pairs being added and the actual pairs, then it returns the sum of each pair. I'm using an array to store the sum of each pair, but when I print out every element the last one is always wrong. This is what I have so far:
int c, val1, val2, x, i;
printf("Enter the number of pairs to sum: \n");
scanf("%d", &c);
int sum [c];
printf("Enter the pairs: \n");
for ( x = 0; x < (c - 1); ++x)
{
scanf("%d", &val1);
scanf("%d", &val2);
sum [x] = val1 + val2;
val1 = 0;
val2 = 0;
}
printf("The sum of each pair is: \n");
for (i = 0; i < c; ++i)
{
printf("%d\t", sum[i]);
}
Change this
for ( x = 0; x < (c - 1); ++x)
to this:
for ( x = 0; x < c; ++x)
You are going until (c-1), where you print c times.
So, after the fix, your loop will be like this:
printf("Enter the pairs: \n");
for ( x = 0; x < (c - 1); ++x)
{
scanf("%d %d\n", &val1, &val2 );
sum [x] = val1 + val2;
val1 = 0; // Do you really need me?
val2 = 0; // and me?
}
You do not need to nullify val1 and val2 at the end of every loop.
Think the first time you enter the loop. These variables are uninitialized, but they get initialized by scanf().
Let's say the user typed 0 and 1, so your variables got these values respectively.
Then, you set them to zero.
Then, you go again inside the loop and the variables get assigned the input of the user.
And so on...
Think how this would work, if you would discard this step:
Then, you set them to zero.
It would be the same!
The only difference is that when you exit the loop, these variables will have what the user typed for the last run of the loop and not the zero values, but I think that's not a problem. :)
I'm just trying to get this program to take a number between 1 and 9 and calculate the radius and display it. This program is supposed to loop but all I'm getting is the "Thank you for using the software!" print function, it's not going through the loop at all and I can't seem to understand why.
#include <stdio.h>
int main(void){
float i,r, V;
for(i=0; i <= 4; i++){
while ( r != 0) {
printf("Enter a radius between 1 and 9: \n");
scanf ("%f", &r);
V= (3.141592)*r*r*10;
if (r>=1 && r<=9){
printf("The cylinder volume is %f\n", V);
}
else if (r > 9 || r < 0){
printf ("The input is out of the acceptable range. Select an integer less than $/n");
}
printf("Thank you for using the software!\n");
}
return 0;
}
You never initialize r before entering your while loop, so this is undefined behavior.
Additionally, you want to use int s for equality operators, i.e. == or !=. In your case, you might want to include an "error" that r can be within.
you have not initialize r before using it.
you must initialize it or use i to iterate though the loop.
INITIALIZE r here before using
for (i = 0; i <= 4; i++)
{
while (r != 0)
{
//code
}
}
To make sure the loop is executed at least once, you could use
do {
// your code
} while ( r != 0);
you better use a do while loop to do that they way you are trying to do.
for(i=0; i <= 4; i++){
do {
printf("Enter a radius between 1 and 9: \n");
scanf ("%f", &r);
V= (3.141592)*r*r*10;
if (r>=1 && r<=9){
printf("The cylinder volume is %f\n", V);
}
else if (r > 9 || r < 0){
printf ("The input is out of the acceptable range. Select an integer less than $/n");
}while ( r != 0)
printf("Thank you for using the software!\n");
}
i haven't tried this by my self.this might work.try it if it works.