Anonymous
i got provided the source code can you help me to check?
This just multiplies 2 matrices. Move the code to multiply 2 matrices into a separate function. Then call it with your first 2 matrices. Use the resultant matrix and your 3rd matrix and call this function again. What is so hard? A better way to do it will be using expression templates and creating your own matrix class. It would be complicated but will be a better design. For your exercise, I think the first way should be good enough.
HaiNahi
Any 3d artists here
ninja
#include <iostream> #include <fstream> using namespace std; int main() { // declaring array variables a, b to hold input matrices // declaring array variable mult to hold the result matrix multiplication int a[100][100], b[100][100], mult[100][100]; // declaring variables r1, c1 to hold the dimensions of matrix a // declaring variables r2, c2 to hold the dimensions of matrix b int r1, c1, r2, c2; // creating an instance of ifstream class to read data from file ifstream infile; // open matrix.dat file to read data of matrices a and b infile.open("matrix.dat"); /* check if matrix.dat file is opened correctly or not if there any error while opening file, report the information to user and stop execution of program */ if (!infile) { cout << " Cannot open the input file."; return 0; } // read the dimensions of matrix a from file infile >> r1 >> c1 ; // read r1 x c1 matrix data from file and store it into array a for(int i = 0; i < r1; i++) for(int j = 0; j < c1; j++) infile >> a[i][j]; // display data of matrix a read from file cout << endl << " Data of Matrix A is: " << endl; for(int i = 0; i < r1; ++i) { for(int j = 0; j < c1; ++j) cout << "\t" << a[i][j]; cout << endl; } // read the dimensions of matrix b from file infile >> r2 >> c2 ; // read r2 x c2 matrix data from file and store it into array b for(int i = 0; i < r2; i++) for(int j = 0; j < c2; j++) infile >> b[i][j]; // display data of matrix b read from file cout << endl << " Data of Matrix B is: " << endl; for(int i = 0; i < r2; i++) { for(int j = 0; j < c2; j++) cout << "\t" << b[i][j]; cout << endl; } /* checking whether multiplication of the matrix can be done If number of columns (c1) of matrix a in not equal to number of rows (r2) of matrix b, then then multiplication of matrix cannot be done */ if (c1 != r2) { cout << " Error! Matrix Multiplication cannot performed"; return 0; } // continuing with matrix multiplication // Initializing elements of matrix mult to 0. for(int i = 0; i < r1; i++) for(int j = 0; j < c2; j++) mult[i][j] = 0; // Multiplying matrix a and b and storing in array mult. for(int i = 0; i < r1; i++) for(int j = 0; j < c2; j++) for(int k = 0; k < c1; k++) mult[i][j] += a[i][k] * b[k][j]; // Displaying the result of matrix multiplication cout << endl << " The result of Matrix Multiplication is : " << endl; for(int i = 0; i < r1; i++) { for(int j = 0; j < c2; j++) cout << "\t" << mult[i][j]; cout << endl; } // writing result of matrix multiplication into matrix.res file // creating an instance of ofstream class to write data into file ofstream outfile; // open matrix.res file to write data of matrix mult into file outfile.open("matrix.res"); /* check if matrix.res file is opened correctly or not if there any error while opening file, report the information to user and stop execution of program */ if (!outfile) { cout << " Cannot open the output file."; return 0; } // otherwise, write mult matrix data into file for(int i = 0; i < r1; i++) { for(int j = 0; j < c2; j++) outfile << mult[i][j] << " "; outfile << "\n"; } return 0; }
this was the most documented code i ever saw on this grp
Murugaa
OK
ninja
Murugaa
yaa
Murugaa
Write a complete C++ program which can multiply three matrices. Your program need to read matrix A , Matrix B, and Matrix C from data file. The size of all the matrices must be at least 3 x 3 Your program must also store the result into result file.
Murugaa
above are the question
ninja
do what M said
Murugaa
🤔🤔🤔
HaiNahi
Any 3d artists here
Anunay
/warn promo
Anonymous
So... why not go ahead and change = to == and see what u get ?
Anonymous
Write a loop that will read a list of numbers and compute the number of even numbers on the list. The list is ended with a sentinel value(such as -1).
Anonymous
Dont wait for others... u just need to type 1 more = and see the result for yourself
Anonymous
Then show the code after u changed = to == ... the latest version
Anonymous
Then what did u get?
Anonymous
That result is?
Anonymous
Say it man... we dont know what u see on your screen...
Anonymous
So what is the problem? If u get 120... is it not what u are expecting?
Anonymous
If u get no errors and the progeam runs and return expected result then code is fixed...
Anonymous
What is fix the shape?
Anonymous
Really ? U got same result for both = and == ? Really? Ok
Hanz
number_1 = number_2 resolves to true number_1 == number_2 will not always resolves to true
The Curious Cat
https://pastebin.com/fPALhwZ3
The Curious Cat
I am running into a segmentation fault while trying to increase the size of the array
mito
Does people use header guards for function prototypes in header files ? The example I have seen used header guard where they used function definition in the header file to not violate the one definition rule.. How would it affect if I do without header guards ?
Anonymous
I am running into a segmentation fault while trying to increase the size of the array
- malloc and realloc may return null pointer. Check it. - Line 23. "n" -> "n-1" to avoid out of bounds. - Line 26. "m-1" -> "m". Otherwise you will get wrong output. - Line 31. Don't free arr. Its invalid. Instead, free arr_new.
Anonymous
Anonymous
nvm multiple declarations arising cuz of not using header guard doesn't affect how the program works... I have tested it now.
If the header only contains function prototype, I think header guards can be removed safely... Maybe By the way, what does "nvm" mean?
Anonymous
Anonymous
Write an algorithm that takes a number and determines whether it is narcissistic. It is a narcissistic number whose sum of digits is equal to the number of digits. For example, 153 is a narcissistic number 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 1 + 125 + 27 = 153 But 1652 is not fascinated 1 ^ 4 + 6 ^ 4 + 5 ^ 4 + 2 ^ 4 = 1 + 1296 + 625 + 16 = 1938
😁😁
Listen
😁😁
First you have to take the count of no. Of digits i.e 153 have 3 digits
Hanz
yes im listening
😁😁
Then after that take a for loop in range 3 and use pow function to calculate (a[i]**n) and add it in a variable called sum
😁😁
And at the end use if to check if sum==no. Or not if yes print the desire output
ian
Noob asks. In C, can I loop through an array using a list slicing like in python, array[1:5] for example? >>> array = ['a', 'b', 'c', 'd', 'e', 'f'] >>> array [1:5] ['b', 'c', 'd', 'e'] >>> array [1:5:2] ['b', 'd']
Ravi
you can using loop but not by using slicing method
😁😁
C/C++?
In this u use while loop to calculate it
😁😁
Calculate no. Of digits
😁😁
And rest is same
Hanz
And at the end use if to check if sum==no. Or not if yes print the desire output
#include <stdio.h> int main() { char i[] = "153"; int n = sizeof i / sizeof char; int sum = 0; for (int i = 0; i < n; i++) sum += a[i] ** n; if (sum == n) printf("%s", "Desire output"); return 0; } Like this?
Hanz
im beginner please enlighten
Ambrose
i want to learn c programming from basic
Stick here. You'll learn
Anonymous
Does anyone know the algorithm and flowchart?
writer
How do you create this program using c++
writer
Write a program that reads the names and average marks of 40 students and assign each of them a grade and then output In a suitable format.
writer
You are given the grading system... In a table ... Kindly anyone help
writer
80-100 grade A 70-80 grade B 60-70 grade C 50-60 grade D 0-50 grade E That is the grading system provided
Pavel
Write a program that reads the names and average marks of 40 students and assign each of them a grade and then output In a suitable format.
Iterate over the input, for each student do if-else-if on the score, push it into a vector After the loop output the data from the vector iterating over it
Talula
How do you create this program using c++
It's simple... by typing.
writer
It's simple... by typing.
😂I know it's typing am asking how do you go about it... The syntax or what conditions should I use to have all the details in the program
writer
Iterate over the input, for each student do if-else-if on the score, push it into a vector After the loop output the data from the vector iterating over it
Could you kindly send a sample.. 😢Can't seem to get any of this... But I had done it with switch statement the vector part is what am not getting
Talula
😂I know it's typing am asking how do you go about it... The syntax or what conditions should I use to have all the details in the program
It is just a simple if statement if (Average>80) grade A if (Average>70 && Average<=80) grade B... it is not so complex
Pavel
Could you kindly send a sample.. 😢Can't seem to get any of this... But I had done it with switch statement the vector part is what am not getting
Can you show the code (you can use ideone or pastebin to paste it here). About the vector, you probably need to declare a struct with the data you need to output. E.g. std::string for name and std::string or char for the grade. Create a vector of these structs, push to it when you decide a mark, then iterate over it to output the data. If you don't know how to use std::vector just Google it, there are a lot of tutorials
Pavel
Or output as you're processing the data
Well, if the input is given line by line, then the input and output will be mixed. If that is OK, then sure, the vector is not needed.
Anonymous
Write an algorithm that takes a number and determines whether it is narcissistic. It is a narcissistic number whose acquisition of digits is equal to the ability of the number of digits against the number itself. For example, 153 is a narcissistic number 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 1 + 125 + 27 = 153 But 1652 is not fascinated 1 ^ 4 + 6 ^ 4 + 5 ^ 4 + 2 ^ 4 = 1 + 1296 + 625 + 16 = 1938 Please answer🙏❤️
Kwabena
in a certain town the transportation fare is dependent on the distance travelled. all passengers pay a fixed amount of 10. in addition passengers are to pay the following. first 10 km is free. next 30 km is 10/km. Next 110 km is 5/km. any km in excess of 150 is 2/km. write a program in c++ that accept a distance travelled in km
Kwabena
Any clue