Tag Archives: সি

C++ code : Read file content

Huge Sell on Popular Electronics

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  ifstream in("test", ios::in | ios::binary);

  if(!in) {
    cout << "Cannot open input file.\n";
    return 1;
  }

  double num;
  char str[80];

  in.read((char *) &num, sizeof(double));
  in.read(str, 14);
  str[14] = '\0'; // null terminate str

  cout << num << ' ' << str;

  in.close();

  return 0;
}

A simple C++ Program code

Huge Sell on Popular Electronics

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  ifstream in("test", ios::in | ios::binary);

  if(!in) {
    cout << "Cannot open input file.\n";
    return 1;
  }

  double num;
  char str[80];

  in.read((char *) &num, sizeof(double));
  in.read(str, 14);
  str[14] = '\0'; // null terminate str

  cout << num << ' ' << str;

  in.close();

  return 0;
}

সি প্রোগ্রামিং এ মেমোরি ব্যবস্থাপনাঃ Memory Management in C Programming

Huge Sell on Popular Electronics

সি প্রোগ্রামিং এ মেমোরি ব্যবস্থাপনা

রিদওয়ান বিন শামীম

এই অধ্যায়ে সি প্রোগ্রামিং এ ডাইনামিক মেমোরি ম্যনেজমেন্ট নিয়ে আলোচনা করা হবে। সি প্রোগ্রামিং ল্যাঙ্গুয়েজে মেমোরি বণ্টন ও ব্যবস্থাপনার জন্য কয়েক ধরনের ব্যবস্থা আছে, তাদেরকে হিডার ফাইলে রাখা হয়। এধরনের ফাংশন গুলোকে এভাবে দেখানো যায়ঃ

S.N. Function and Description

1 void *calloc(int num, int size);
Num ইলিমেন্টের এর জন্য array বরাদ্দ দেয় এই ফাংশন, যেগুলোর আকার বাইটে সীমাবদ্ধ।

2 void free(void *address);
এই ফাংশন এড্রেস দ্বারা নির্ধারিত মেমোরি ব্লক প্রকাশ করে।

3 void *malloc(int num);
এই ফাংশন num bytes এর array বরাদ্ধ করে,এবং তাদের শুরু করায়।

4 void *realloc(void *address, int newsize);
এই ফাংশন মেমোরিকে পুনরায় সংগঠিত করে ও নতুন আকারে গড়ে তুলে।

প্রোগ্রামিং এ কাজ করার সময় যদি অ্যারির আকার সম্পর্কে জানা থাকে তাহলে অ্যারি সনাক্ত করা সহজ হবে। উদাহরণস্বরূপ, কারো নাম সংরক্ষণ করতে গেলে, নাম লিখতে ম্যাক্সিমাম ১০০ অক্ষর লাগতে পারে, যা আমরা এভাবে লিখতে পারি,
char name[100];

কিন্তু যদি এমন হয় যেখানে আমাদের যে তথ্য লিখতে হবে তার আকার সম্পর্কে কোন ধারণা নেই, যেমন কোন বিষয়ে বিবৃতি, সেক্ষেত্রে আমাদের একটি পয়েন্টার ব্যবহার করতে হবে অনির্ধারিত মেমোরি সাইজ উল্লেখ করে, যা পরবর্তীতে এভাবে দেখানো যায়,
#include
#include
#include

int main()
{
char name[100];
char *description;

strcpy(name, "Zara Ali");

/* allocate memory dynamically */
description = malloc( 200 * sizeof(char) );
if( description == NULL )
{
fprintf(stderr, "Error - unable to allocate required memory\n");
}
else
{
strcpy( description, "Zara ali a DPS student in class 10th");
}
printf("Name = %s\n", name );
printf("Description: %s\n", description );
}

উপরের কোডগুলো ঠিকভাবে লিখা হলে তা এরুপ ফলাফল দেখাবে,
Name = Zara Ali
Description: Zara ali a DPS student in class 10th

একই ধরনের প্রোগ্রাম লিখা যাবে malloc কে calloc দ্বারা রিপ্লেস করে,
calloc(200, sizeof(char));

অপারেটিং সিস্টেম নিজেই প্রোগ্রামের বরাদ্দ করা মেমোরি রিলিজ করবে, কিন্তু আমরা নিজেরাই প্রয়োজন শেষ হলে free() ফাংশন দ্বারা মেমোরি রিলিজ করে দিতে পারি। এছাড়াও realloc() ফাংশন ব্যবহার করে বরাদ্দকৃত মেমোরি ব্লকের আকার ছোটবড়ও করা যায়। realloc() এবং free() ফাংশন ব্যবহার করে দেখা যাক।

#include
#include
#include

int main()
{
char name[100];
char *description;

strcpy(name, "Zara Ali");

/* allocate memory dynamically */
description = malloc( 30 * sizeof(char) );
if( description == NULL )
{
fprintf(stderr, "Error - unable to allocate required memory\n");
}
else
{
strcpy( description, "Zara ali a DPS student.");
}
/* suppose you want to store bigger description */
description = realloc( description, 100 * sizeof(char) );
if( description == NULL )
{
fprintf(stderr, "Error - unable to allocate required memory\n");
}
else
{
strcat( description, "She is in class 10th");
}

printf("Name = %s\n", name );
printf("Description: %s\n", description );

/* release memory using free() function */
free(description);
}

উপরের কোড ঠিকভাবে লিখা হলে তা যে ফলাফল দেখাবে তা হল,
Name = Zara Ali
Description: Zara ali a DPS student.She is in class 10th
বাড়তি মেমোরি ব্লক বরাদ্দ না করে আমরা উপরের ফাংশন লিখে দেখতে পারি, মেমোরিতে সীমাবদ্ধতা থাকলে strcat() ফাংশন প্রদর্শিত হবে।

সি এবং অ্যারে : Array in C

Huge Sell on Popular Electronics

int marks[4][10] = {{80, 70, 92, 78, 58, 83, 85, 66, 99, 81}, {75, 67, 55, 100, 91, 84, 79, 61, 90, 97}, {98, 67, 75, 89, 81, 83, 80, 90, 88, 77}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};

 

 #include <stdio.h>  
 int main()  
 {  
     int marks[4][10] = {{80, 70, 92, 78, 58, 83, 85, 66, 99, 81}, {75, 67, 55, 100, 91, 84, 79, 61, 90, 97}, {98, 67, 75, 89, 81, 83, 80, 90, 88, 77}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};  
     int col;  
     for(col = 0; col < 10; col++) {  
         marks[3][col] = marks[0][col] / 4.0 + marks[1][col] / 4.0 + marks[2][col] / 2.0;  
         printf("Roll NO: %d  Total Marks: %d\n", col + 1, marks[3][col]);  
     }  
     return 0;  
 }

 

 int marks[4][10];  
 int i, j;  
 for (i = 0; i < 4; i++) {  
     for (j = 0; j < 10; j++) {  
         scanf("%d", &ara[i][j]);  
     }  
 } 

 

#include <stdio.h>  
 int main()  
 {  
     int namta[10][10];  
     int row, col;  
     for (row = 0; row < 10; row++) {  
         for(col = 0; col < 10; col++) {  
             namta[row][col] = (row + 1) * (col + 1);  
         }  
     }  
     for (row = 0; row < 10; row++) {  
         for(col = 0; col < 10; col++) {  
             printf("%d x %d = %d\n", (row + 1), (col + 1), namta[row][col]);  
         }  
         printf("\n");  
     }  
     return 0;  
 }  
#include <stdio.h>  
 int main()  
 {  
     char saarc[7][100] = {"Bangladesh", "India", "Pakistan", "Sri Lanka", "Nepal", "Bhutan", "Maldives"};  
     int row;  
     for (row = 0; row < 7; row++) {  
         printf("%s\n", saarc[row]);  
     }  
     return 0;  
 }  

 

 

#include <stdio.h>  
 #include <string.h>  
 int main()  
 {  
     char saarc[7][100] = {"Bangladesh", "India", "Pakistan", "Sri Lanka", "Nepal", "Bhutan", "Maldives"};  
     int row, col, name_length;  
     for (row = 0; row < 7; row++) {  
         name_length = strlen(saarc[row]);  
         for(col = 0; col < name_length; col++) {  
             printf("%c ", saarc[row][col]);  
         }  
         printf("\n");  
     }  
     return 0;  
 }  


#include <stdio.h>  
 #include <string.h>  
 int main()  
 {  
     char saarc[7][100] = {"Bangladesh", "India", "Pakistan", "Sri Lanka", "Nepal", "Bhutan", "Maldives"};  
     int row, col, name_length;  
     for (row = 0; row < 7; row++) {  
         name_length = strlen(saarc[row]);  
         for(col = 0; col < name_length; col++) {  
             printf("(%d, %d) = %c, ", row, col, saarc[row][col]);  
         }  
         printf("\n");  
     }  
     return 0;  
 }  
 
 #include <stdio.h>  
 #include <string.h>  
 int main()  
 {  
     int ara1[5][5] = {{1, 2, 3, 4, 5}, {10, 20, 30, 40, 50}, {100, 200, 300, 400, 500}, {1000, 2000, 3000, 4000, 5000}, {10000, 20000, 30000, 40000, 50000}};  
     int ara2[5][5];  
     int r, c;  
     printf("Content of first array (ara1): \n");  
     for (r = 0; r < 5; r++) {  
         for(c = 0; c < 5; c++) {  
             printf("%d ", ara1[r][c]);  
         }  
         printf("\n");  
     }  
     printf("\n");  
     // now start copy  
     for (r = 0; r < 5; r++) {  
         for(c = 0; c < 5; c++) {  
             ara2[c][r] = ara1[r][c];  
         }  
     }  
     printf("Content of second array (ara2): \n");  
     for (r = 0; r < 5; r++) {  
         for(c = 0; c < 5; c++) {  
             printf("%d ", ara2[r][c]);  
         }  
         printf("\n");  
     }  
     return 0;  
 }  


	

লেকচার-০৫: সি++ প্রোগ্রামিং – একটি সম্পূর্ণ প্রোগ্রাম লেখা (Hands on Programming)

Huge Sell on Popular Electronics

লেকচার – ০৪: সি++ প্রোগ্রামিং – লজিক (C++ Programming – Logic)

Huge Sell on Popular Electronics

লেকচার – ০৩: সি++ প্রোগ্রামিং- ভেরিয়েবল এবং এক্সপ্রেশন (Variables and Expressions)

Huge Sell on Popular Electronics

লেকচার-০২: সি++ প্রোগ্রামিং – আপনার প্রথম প্রোগ্রাম (C++ Programming – Your first program)

Huge Sell on Popular Electronics

লেকচার – ০১: সি++ প্রোগ্রামিং – মেশিনের মত চিন্তা করুন (C++ Programming – Think like a machine)

Huge Sell on Popular Electronics

লেকচার – 00: সি++ প্রোগ্রামিং কোর্সের রুপরেখা (C++ Programming Course Outline)

Huge Sell on Popular Electronics

লেকচার-০৭: সিএসই-১০০: সি প্রোগ্রামিং পরিচিতি – সি এ লুপ (For loop in C)

Huge Sell on Popular Electronics

লেকচার-০৬: সিএসই-১০০: সি প্রোগ্রামিং পরিচিতি – হোয়াইল লুপ, (While loop in C)

Huge Sell on Popular Electronics

লেকচার-০৮: সিএসই-১০০: সি প্রোগ্রামিং পরিচিতি – সি এ ফাংশন / মেথড (Function/method in C)

Huge Sell on Popular Electronics

লেকচার-০৫: সিএসই-১০০: সি প্রোগ্রামিং পরিচিতি – সি এ শর্তাধীন বিবৃতি (Conditional Statements in C)

Huge Sell on Popular Electronics

লেকচার-০৪: সিএসই-১০০: সি প্রোগ্রামিং পরিচিতি – ইনপুট, অ্যারে, বুলিয়ান এক্সপ্রেশন; আপনার প্রথম প্রোগ্রাম (Input, Array, Boolean Expression; your first program)

Huge Sell on Popular Electronics

Input

Array

Boolean Expression

লেকচার-০৩: সিএসই-১০০: সি প্রোগ্রামিং পরিচিতি – মেমরি এবং ভেরিয়েবল; আপনার প্রথম প্রোগ্রাম, (Memory and Variables; your first program)

Huge Sell on Popular Electronics

লেকচার-০২: সিএসই-১০০: সি প্রোগ্রামিং পরিচিতি – অভিব্যক্তি, ভেরিয়েবল, এবং সংরক্ষিত শব্দ, কোর্স (Expression, Variables, and Reserved Words)

Huge Sell on Popular Electronics

Expression and Variables

Reserved Words

লেকচার-০১: সিএসই-১০০: সি প্রোগ্রামিং পরিচিতি – কম্পিউটার প্রোগ্রামিং এবং ডেটার প্রকারভেদ, (Computer Programming and Data Types)

Huge Sell on Popular Electronics

কম্পিউটার প্রোগ্রামিং এবং ডেটার প্রকারভেদ