Said Dulevic

Msc Said Dulevic

Web Developer & Lecturer

Strukture te dhenash – Ushtrime dhe detyra

animationss

Ushtrime nga Strukture te dhenash ne C & C++

Konvertoni nje numer decimal ne numer binar me ane te funskionit rekrusiv

#include  
using namespace std; 


int find(int decimal_number) 
{ 
	if (decimal_number == 0) 
		return 0; 
	else
		return (decimal_number % 2 + 10 * 
				find(decimal_number / 2)); 
} 

int main() 
{ 
	int decimal_number = 10; 
	cout << find(decimal_number); 
	return 0; 
} 

Afishoni ne console permbajtjen e skedarit te ndodhur ne direktorine D:\\students.txt

#include 
#include 

using namespace std;

int main(){
  ofstream file;
  file.open ("D:\students.txt"); //hap skedarin
  return 0;
}

Shtoni ne fund te skedarit students.txt nota per m studente te tjere te cilat do i lexoni nga perdoruesi


#include 
#include 

using namespace std;

int main(){

  char text[200];

  fstream file;
  file.open ("D:\students.txt", ios::out | ios::in );

  cout << "Shtoni notat per studentet e tjere." << endl;
  cin.getline(text, sizeof(text));

  // Shkruan ne file
  file << text << endl;

  // Lexon ne file
  file >> text;
  cout << text << endl;

  //mbyll file
  file.close();
  return 0;
}

Ndertoni nje funksion rekrusiv i cili gjen mesataren e elementeve cift ne nje vektor

#include 
#include 

int main()
{
    int arr[6]={5,10,15,20,25,30};   
    int i,oddSum=0,evenSum=0, oddCount=0,evenCount=0;
    double avgOdd,avgEven;

    for(i=0; i<6; i++){  
        if(arr[i]%2==0){
        evenSum=evenSum+arr[i]; 
        evenCount++;
        }
    else{
             oddSum=oddSum+arr[i];  
             oddCount++;
        }
    }
    avgOdd=oddSum/oddCount;  
    avgEven=evenSum/evenCount;
    printf("Mesatarja e numrave tek: %.2f",avgOdd);  
    printf("\nMesatarja e numrave cift: %.2f",avgEven);
    getch();
    return 0;
}

Gjeni numrin e zanoreve ne ne skedar text

auto isvowel = [](char c){ return c == 'A' || c == 'a' ||
                                  c == 'E' || c == 'e' ||
                                  c == 'I' || c == 'i' ||
                                  c == 'O' || c == 'o' ||
                                  c == 'U' || c == 'u'; };

std::ifstream f("file.txt");

auto numVowels = std::count_if(std::istreambuf_iterator(f),
                               std::istreambuf_iterator(),
                               isvowel);

Gjeni zanoret dhe bashketingelloret ne nje skedar text


#include iostream
#include fstream
#include set
#include algorithm
using namespace std;

int main() {
    set zanoret = { 'a', 'e', 'i', 'o', 'u' };
    set bsht = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x', 'z', 'w', 'y' };
    set tegjitha = { 'a', 'e', 'i', 'o', 'u', 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x', 'z', 'w', 'y' };
    char ch;
    int nrzanoreve = 0;
    int nrbsht = 0;
    int perqzanoreve = 0;
    int perqbsht = 0;
    int nrshkronjave = 0;
    fstream fin("MyFile.txt", fstream::in);

    while (fin >> ch) {
        ch = tolower(ch);
        if (find(tegjitha.begin(), tegjitha.end(), ch) != tegjitha.end())
        {
            nrshkronjave++;
        }
        if (find(zanoret.begin(), zanoret.end(), ch) != zanoret.end())
        {
            nrzanoreve++;
        }
        if (find(bsht.begin(), bsht.end(), ch) != bsht.end())
        {
            nrbsht++;
        }
    }
    perqzanoreve = double(nrzanoreve) / nrshkronjave * 100;
    perqbsht = double(nrbsht) / nrshkronjave * 100;
    cout << "Zanore     %: " << perqzanoreve << endl;
    cout << "Bashktingellore %: " << perqbsht << endl;
    getchar();
    return 0;
}

Denduria shkronjave ne nje file


#include iostream
include fstream
using namespace std;
int main()
{
        ifstream  fin("read.txt");
        char ch;
        int i, c=0, sp=0;
        while(fin)
        {
                fin.get(ch);
                i=ch;
                if((i > 63 && i < 91) || (i > 96 && i < 123))
                        c++;
                else
                        if(ch== ' ')
                                sp++;
        }
        cout<<"\n No. shkronjave ne nje file : "<

Bashkimi i permbajtjes se dy fileve file1.txt me file2.txt


#include  bits/stdc++.h
using namespace std; 

int main() 
{ 
	fstream file; 

	
	ifstream ifile("file.txt", ios::in); 

	ofstream ofile("file2.txt", ios::out | ios::app); 

	if (!ifile.is_open()) { 

		cout << "file not found"; 
	} 
	else { 
		ofile << ifile.rdbuf(); 
	} 
	string word; 
	file.open("file2.txt"); 

	while (file >> word) { 

		cout << word << " "; 
	} 

	return 0; 
} 

E kunderta e nje liste


#include  
using namespace std; 

struct Node { 
	int data; 
	struct Node* next; 
	Node(int data) 
	{ 
		this->data = data; 
		next = NULL; 
	} 
}; 

struct LinkedList { 
	Node* head; 
	LinkedList() 
	{ 
		head = NULL; 
	} 

	void reverse() 
	{ 

		Node* current = head; 
		Node *prev = NULL, *next = NULL; 

		while (current != NULL) { 
			// Store next 
			next = current->next; 

			// Reverse current node's pointer 
			current->next = prev; 

			// Move pointers one position ahead. 
			prev = current; 
			current = next; 
		} 
		head = prev; 
	} 


	void print() 
	{ 
		struct Node* temp = head; 
		while (temp != NULL) { 
			cout << temp->data << " "; 
			temp = temp->next; 
		} 
	} 

	void push(int data) 
	{ 
		Node* temp = new Node(data); 
		temp->next = head; 
		head = temp; 
	} 
}; 


int main() 
{ 

	LinkedList ll; 
	ll.push(20); 
	ll.push(4); 
	ll.push(15); 
	ll.push(85); 

	cout << "Given linked list\n"; 
	ll.print(); 

	ll.reverse(); 

	cout << "\nReversed Linked list \n"; 
	ll.print(); 
	return 0; 
} 

Krijoni nje funskion i cili shton nje node ne nje liste te renditur


#include  
using namespace std; 
  
/* Krijimi listes */
class Node  
{  
    public: 
    int data;  
    Node* next;  
};  
  
/* funksion për të futur një new_node në një listë. Vini re se kjo
funksioni pret një tregues në head_ref pasi kjo mund të modifikojë
kreu i listës së lidhur me hyrjen (e ngjashme me push ())*/
void sortedInsert(Node** head_ref, Node* new_node)  
{  
    Node* current;  
   / * Rast special për fundin e kokës * /
    if (*head_ref == NULL || (*head_ref)->data >= new_node->data)  
    {  
        new_node->next = *head_ref;  
        *head_ref = new_node;  
    }  
    else
    {  
       / * Gjeni nyjen para pikës së futjes * /
        current = *head_ref;  
        while (current->next!=NULL &&  
            current->next->data < new_node->data)  
        {  
            current = current->next;  
        }  
        new_node->next = current->next;  
        current->next = new_node;  
    }  
}  
  

Node *newNode(int new_data)  
{  

    Node* new_node =new Node(); 
  

    new_node->data = new_data;  
    new_node->next = NULL;  
  
    return new_node;  
}  
  

void printList(Node *head)  
{  
    Node *temp = head;  
    while(temp != NULL)  
    {  
        cout<data<<" ";  
        temp = temp->next;  
    }  
}  

int main()  
{  
 
    Node* head = NULL;  
    Node *new_node = newNode(5);  
    sortedInsert(&head, new_node);  
    new_node = newNode(10);  
    sortedInsert(&head, new_node);  
    new_node = newNode(7);  
    sortedInsert(&head, new_node);  
    new_node = newNode(3);  
    sortedInsert(&head, new_node);  
    new_node = newNode(1);  
    sortedInsert(&head, new_node);  
    new_node = newNode(9);  
    sortedInsert(&head, new_node);  
    cout<<"Created Linked List\n";  
    printList(head);  
  
    return 0;  
}  

Info shtese

error: Alert: Ups materiali eshte i bllokuar!!
Need Help? Chat with me