Hi I have this code written to read in a matrix with the dimension given. However I want to modify it to read in a matrix from a file from the command line using cin. I then want it to print row by row the position of a non zero element followed by that value. Can someone help me modify this. Thanks.
#include <fstream>
#include <iostream>
#include <stdlib.h>
//using std:#include <fstream>
//#include <iostream>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::ofstream;
void readArray(int, int, double **);
int nz=0;
main(int argc, char *argv[])
{
int rowCT = atoi(argv[1]);
int colCT = atoi(argv[2]);
// here you reserve the space for the array
double **A = new double*[rowCT];
for (int i = 0; i < rowCT; i++)
A[i] = new double[colCT];
readArray(rowCT, colCT, A);
}
//int count = new int[rowCT];
void readArray(int r, int c, double **arr)
{
//here r rows of c elements are read in
int count[r];
int total=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cin>> arr[i][j];
if(arr[i][j]>0)
{
nz++;
}
}
count[i]=nz;
nz = 0;
}
cout<< r << endl;
for(int i=0; i<r; i++)
{
cout<<count[i]<< " ";
for(int j=0; j<c; j++)
{
if(arr[i][j]>0)
{
cout<< j+1 << " " << arr[i][j] << " ";}
}
cout<< endl;
}
for(int i =0; i<r; i++)
{
total+= count[i];
}
cout << total<< endl;
}
Related
The Function won't initiate can someone help? When I run it in the debugger program skips over function and I don't know why?
#include <iostream>
using namespace std;
int size_array= 0;
int *data_array;
void sorting(int *[], int);
int main()
{
cout<<"enter in array size \n";
cin>>size_array;
int *data_array=new int(size_array);
for(int i=0;i<size_array;i++)
{
cout<<"enter number "<<i+1<<endl;
cin>>data_array[i];
}
**int sorting(int data_array, int size_array);**
for (int i=0; i<size_array;i++)
{
cout<<data_array[i]<<endl;
}
return 0;
}
The marked code is simply declaring the function, not calling it.
Also, your data_array is a pointer to a single int whose value is initialized as size_array. But you want an array of size_array number of ints instead.
Try this:
#include <iostream>
using namespace std;
void sorting(int[], int);
int main()
{
int size_array = 0;
cout << "enter in array size \n";
cin >> size_array;
int *data_array = new int[size_array];
for(int i = 0; i < size_array; i++)
{
cout << "enter number " << i+1 << endl;
cin >> data_array[i];
}
sorting(data_array, size_array);
for (int i = 0; i < size_array; i++)
{
cout << data_array[i] << endl;
}
delete[] data_array;
return 0;
}
void sorting(int data_array[], int size_array)
{
// sort data_array as needed...
}
I have some code written but I'm not sure why the reversed array is not giving me the exact values I need. I created a second array the same size as the first and used nested for loops to fill the second with the contents of the first in reverse.
See below:
#include <iostream>
using namespace std;
int main()
{
// Ask for how big the array is
int n;
cout << "how big is the array?" << endl;
cin >> n;
// create array
int a[n];
// create second array
int b[n];
// ask for contents of the 1st array
cout << "what's in the array?" << endl;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
// reverse the array
for (int i = n - 1; i >= 0; i--)
{
for (int k = 0; k < n; k++)
{
b[k] = a[i];
break;
}
}
// print out the new array
for (int k = 0; k < n; k++)
{
cout << b[k] << endl;
}
return 0;
}
you don't need 2 bucles for fill the second array
try with:
//reverse the array
s = 0;
for (int i=n-1;i>=0;i--){
b[n]=a[s];
s++;
}
Try something like this:
#include <algorithm>
#include <iostream>
#include <vector>
namespace {
template <typename IStream>
[[nodiscard]] int readOneIntFrom(IStream& istream) {
int x;
istream >> x;
return x;
}
}
int main()
{
// Ask for how big the array is
std::cout << "how big is the array?" << std::endl;
auto n = readOneIntFrom(std::cin);
// create array
std::vector<int> a;
// ask for contents of the 1st array
std::cout << "what's in the array?" << std::endl;
for (int i = 0; i < n; i++)
{
a.emplace_back(readOneIntFrom(std::cin)); // Make a new entry at the end of a.
}
// Construct b from a backward. (Or do auto b = a; std::reverse(b.begin(), b.end());
auto b = std::vector<int>(a.rbegin(), a.rend());
// print out the new array
for (const auto& bi : b)
{
std::cout << bi << std::endl;
}
return 0;
}
i made this program to make a function to check if integar array is palindrome or not.it is always giving output that it is not a palidrome on every input. can you plz help me out?
code:
#include<iostream>
using namespace std;
void palindrome(int[],int[],int);
int main()
{
int size=5;
int array1[size];
int array2[size];
palindrome(array1,array2,size);
}
void palindrome(int array1[],int array2[],int size)
{
size=5;
int l=0;
cout<<"enter your array=";
for(int i=0;i<size;i++)
{
cin>>array1[i];
}
for(int i=0;i<size;i++)
{
array2[i]=array1[size-i];
}
if(array1==array2)
{
cout<<"given array is palindrome.";
}
else{
cout<<"given array is not palindrome.";
}
}
You can't compare two arrays with ==, it will only compare if the array's address are equals, so different arrays never equals, we can switch it to std::equal
There is one memory issue with array2[i]=array1[size-i];, you will get a buffer overflow.
This is a slighly modified version of your code:
#include <iostream>
using namespace std;
void palindrome(int[], int[], int);
int main() {
int size = 5;
int array1[size];
int array2[size];
palindrome(array1, array2, size);
}
void palindrome(int array1[], int array2[], int size) {
size = 5;
int l = 0;
cout << "enter your array=";
for (int i = 0; i < size; i++) {
cin >> array1[i];
}
for (int i = 0; i < size; i++) {
array2[i] = array1[size - i - 1];
}
if (std::equal(array1, array1 + size, array2, array2 + size)) {
cout << "given array is palindrome.";
} else {
cout << "given array is not palindrome.";
}
}
It's recommended to use std::vector instead of the C style array:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void palindrome(int);
int main() {
int size = 5;
palindrome(size);
}
void palindrome(int size) {
std::vector<int> vec(size);
cout << "enter your array=";
for (int i = 0; i < size; i++) {
cin >> vec[i];
}
std::vector<int> rev_rec{vec.rbegin(), vec.rend()};
if (vec == rev_rec) {
cout << "given array is palindrome." << std::endl;
} else {
cout << "given array is not palindrome." << std::endl;
}
}
And we can also avoid the copy of vector, with reverse iterator:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void palindrome(int);
int main() {
int size = 5;
palindrome(size);
}
void palindrome(int size) {
std::vector<int> vec(size);
cout << "enter your array=";
for (int i = 0; i < size; i++) {
cin >> vec[i];
}
if (std::equal(vec.begin(), vec.end(), vec.rbegin(), vec.rend())) {
cout << "given array is palindrome." << std::endl;
} else {
cout << "given array is not palindrome." << std::endl;
}
}
should be like this output:
Monday
onday
nday
day
ay
y
what I have so far:
#include <iostream>
using namespace std;
int main () {
char *weekDays[7]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
for (int i=0;i<7;i++){
cout << weekDays[0][i] << endl;
}
return 0;
}
output:
M
o
n
d
a
y
Im on mobile and tired but sth. like this should work:
int main(){recPrint(0);}
void recPrint(int level){
char mon[] = "Monday";
for(int i=level; i<strlen(mon); i++){
std::cout << mon[i];
}
std::cout << std::endl;
recPrint(++level);
}
add another parameter char dayOfTheWeek[] and you can call it with whatever you want.
Try this
#include <iostream>
using namespace std;
void display(char s[])
{
int n = strlen(s);
for(int i = 0; i < n; i++)
{
for(int j = i; j < n; j++)
{
cout << s[j];
}
cout << endl;
}
}
int main () {
char *weekDays[7]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
display(weekDays[0]);
}
I'm trying to implement this sorting algorithm: http://en.wikipedia.org/wiki/Library_sort. I have some functions grupBul for finding suitable group, grupSirala for sort every group. I have to leave some gaps and contain them in a vector another data structure.
How can I perform this? So far I have this code:
#include "librarySort.h"
#include <iostream>
#include <vector>
#include <math.h>
#include "sort.h"
using namespace std;
#define EPSILON 10 //Epsilon sabiti
#define GRUPDEFBOYUT 20 //Gruplarin baslangiƧ boyutu
//gruplar[grupno][degerler] grupboyutu->[0] minimumeleman->[1] maksimumeleman->[2]
vector<vector<int> > gruplar;
int grupBul(vector<int*> &dizi,int sayi){
for(int i=0;i<gruplar.size();i++){
if(*dizi[gruplar[i][1]]<sayi && *dizi[gruplar[i][2]]>sayi){
return gruplar[i][2];
}
}
for(int i=gruplar.size()-1;i>=0;i--){
if (*dizi[gruplar[i][2]]<sayi){
return gruplar[i][2];
}
}
if(*dizi[gruplar[0][1]]>=sayi){
return gruplar[0][2];
}
}
int maxBul(vector<int*> &dizi,int baslangic,int son){
int max=*dizi[baslangic];
int index=baslangic;
for(int i=baslangic+1;i<son;i++){
if(*dizi[i]>max){
max=*dizi[i];
index=i;
}
}
return index;
}
int minBul(vector<int*> &dizi,int baslangic,int son){
int min=*dizi[baslangic];
int index=baslangic;
for(int i=baslangic+1;i<son;i++){
if(*dizi[i]<min){
min=*dizi[i];
index=i;
}
}
return index;
}
void ilkGruplariOlustur(vector<int*> &dizi,int son){
int grupboyut=0;
int grupno=0;
for(int i=0;i<5;i++){
vector<int> grup;
grup.push_back(GRUPDEFBOYUT);
grup.push_back(minBul(dizi,grupboyut,GRUPDEFBOYUT+grupboyut));
grup.push_back(maxBul(dizi,grupboyut,GRUPDEFBOYUT+grupboyut));
gruplar.push_back(grup);
/*for(int j=0;j<EPSILON;j++)
dizi.push_back(NULL);*/
grupboyut+=GRUPDEFBOYUT;
grupno++;
}
}
void grupSirala(vector<int*> &dizi,int baslangic,int son){
int j = 0;
int mover;
for ( int i = baslangic + 1 ; i < son ; i++ ) {
mover = *dizi[i];
j = i-1;
while ( ( j >= 0 ) && ( *dizi[j] > mover) ){
*dizi[j+1] = *dizi[j];
j--;
}
*dizi[j+1] = mover;
}
}
void bosluklariOlustur(vector<int*> &dizi){
}
int librarySort( int *data , unsigned int size) {
int kok_n=(int)sqrt((double)size);
int boyut=size;
vector<int*> sirali_dizi;
for(int i=0;i<kok_n;i++){
sirali_dizi.push_back(new int(*(data+i)));
}
ilkGruplariOlustur(sirali_dizi,0);
for(int i=0;i<sirali_dizi.size();i++)
cout << *sirali_dizi.at(i)<< " ";
for(int a = 0;a<10;a++)
sirali_dizi.push_back(NULL);
grupSirala(sirali_dizi,0,kok_n);
for(int i=0;i<5;i++){
for(int j=0;j<3;j++)
cout << gruplar[i][j] << " ";
}
/*
cout<<endl<<"Sirali dizi"+i<<endl;
for(int t=0;t<20*(i+1)+EPSILON;t++)
cout << *sirali_dizi.at(t)<< " ";
cout<<endl;
}
cout<<endl;
cout<<endl;
cout<<endl;
grupSirala(sirali_dizi,0,sirali_dizi.size());*/
for(int i=0;i<sirali_dizi.size();i++)
cout << *sirali_dizi.at(i)<< " ";
return 0;
}
Well, vector has a resize method that will allow you to basically preset the vector's size before inserting data. Then from there you'll be able to use the operator[] to insert data at the appropriate spaces.
As a side note, I personally would not use STL if I'm working on this project/homework.